这是我的model,serializer和'admin`‘
class UserData(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    pub_date = models.DateTimeField('date published',default=timezone.now)   
class UserDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = Genre
        fields = ('id','pub_date')
    def create(self, validated_data):
        user_data = UserData()
        user_data.save()
        return user_data
class UserDataAdmin(admin.ModelAdmin):
    list_display = ["id","pub_date"]
    search_fields = ['id']在管理屏幕中,id字段如下所示

但在rest-framework中,id显示为整数。

我该怎么解决这个问题??
感谢您的@periwinkle评论
我变了
class UserDataSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(format='hex_verbose') // add this
    class Meta:
        model = Genre
        fields = ('id','pub_date')
    def create(self, validated_data):
        user_data = UserData()
        user_data.save()
    return user_data在此之后,当POST url创建新的数据。
它显示错误
HTTP 400 Bad Request
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
    "id": [
        "This field is required."
    ]
}但是id有缺省值,所以有问题。
发布于 2021-03-12 13:46:00
似乎您可以在模型中显式指定uuid格式。这是来自drf docs关于UUID的内容:
Signature: UUIDField(format='hex_verbose')
format: Determines the representation format of the uuid value
'hex_verbose' - The canonical hex representation, including hyphens: "5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
'hex' - The compact hex representation of the UUID, not including hyphens: "5ce0e9a55ffa654bcee01238041fb31a"
'int' - A 128 bit integer representation of the UUID: "123456789012312313134124512351145145114"
'urn' - RFC 4122 URN representation of the UUID: "urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a" Changing the format parameters only affects representation values. All formats are accepted by to_internal_valuehttps://stackoverflow.com/questions/66594627
复制相似问题