2

我设置了一个名为 CustomerViewset 的 Django Rest Framework (v.1.11.7) 视图集。简单如下:

class CustomerViewSet(viewsets.ModelViewSet):
  queryset = Customer.objects.all()
  serializer_class = CustomerSerializer

序列化器看起来像:

class CustomerSerializer(serializers.ModelSerializer):
  class Meta:
    model = Customer
    fields = ('id', 'name', 'zone', 'website', 'email', 'slug', 'active', 'description', 'primary_color', 'nif', 'country', 'city', 'zip_code', 'address_1', 'address_2', 'address_3', 'telephone', 'allow_delete')

和模型:

class Customer(models.Model):
  name = models.CharField(
    max_length=256
  )
  zone = models.ForeignKey(
    Zone,
    null=True,
    blank=True
  )
  active = models.BooleanField(
    default=True
  )
  allow_delete = models.BooleanField(
    default=True
  )
  logo = models.ImageField(
    null=True,
    blank=True,
    upload_to="customer/logo"
  )
  description = models.TextField(
    blank=True
  )
  website = models.URLField(
    blank=True
  )
  email = models.EmailField(
    blank=True
  )
  telephone = models.CharField(
    max_length=256,
    null=True,
    blank=True
  )
  slug = models.SlugField(unique=True, blank=True, null=True)
  nif = models.CharField(max_length=32, null=True, blank=True)
  country = models.CharField(max_length=128, null=True, blank=True)
  city = models.CharField(max_length=128, null=True, blank=True)
  zip_code = models.CharField(max_length=128, null=True, blank=True)
  address_1 = models.CharField(max_length=256, null=True, blank=True)
  address_2 = models.CharField(max_length=256, null=True, blank=True)
  address_3 = models.CharField(max_length=256, null=True, blank=True)
  primary_color = models.CharField(max_length=7, default="#F8F9FA")

这应该没有问题,但是在对 Customer 对象进行更改(更新或部分更新)之后,将不会再次列出更新的特定对象。导致问题的更新有效负载具有以下结构:

active  true
address_1   null
address_2   null
address_3   null
allow_delete    true
city    null
country null
description 
email   
id  818
name    'Name test'
nif null
primary_color   '#F8F9FA'
slug    'name-test'
telephone   null
website 
zip_code    null
zone    1

甚至没有重新启动服务器(缓存问题)。但它仍然可以检索。控制台或日志上没有显示错误;列出时它只是忽略记录。

通过覆盖get_querysetViewset 我可以执行以下操作:

    def get_queryset(self):
      queryset = Customer.objects.all()
      c = Customer.objects.get(id=818)
      print(c)
      _c = queryset.filter(id=818)
      print(_c)
      return queryset

818 是更新记录的id。它确实通过过滤查询集和客户模型找到了。但是,它仍然不在列表中!

这真的很奇怪,不知道我在这里错过了什么。任何想法将不胜感激。

4

0 回答 0