在Django中,我使用filter
返回查询集,以便可以在序列化程序中对其进行更新。问题是我遇到以下错误:'QuerySet' object has no attribute 'vin'
。我了解当您尝试在Queryset上查找字段时发生错误,在这种情况下,您将使用get()
而不是filter()
,但是我没有进行字段查找。我的数据库确实得到了预期的更新,但是无论如何我都会收到错误消息。 docs的使用方式相同:
# Update all the headlines with pub_date in 2007.
Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')
。但是我不确定为什么它仍然像在尝试在查询集上进行字段查找一样
serializers.py
class ShoppingListSerializer(serializers.ModelSerializer):
class Meta:
model = ShoppingList
fields = ('vin', 'img_url', 'year', 'make', 'model', 'grade',
'colour', 'MMR', 'run_date', 'timestamp', 'lane', "trim",
"mileage", 'human_valuation', 'run_no', 'adesa_id', 'engine',
'transmission', 'wheel_drive', 'interior_color', 'seller_announcements',
'auction_location', 'extra', 'check')
def create(self, validated_data):
# look up the supplied vin, rundate and check fields before POSTing
# if the instance exists then just update
vin = validated_data["vin"]
run_date = validated_data["run_date"]
check = validated_data["check"]
lookup = ShoppingList.objects.filter(vin=vin, run_date=run_date, check=check)
# if lookup exists then update that instance instead
if lookup:
print("Updating old record")
lookup.update(**validated_data)
print(lookup)
return lookup
return ShoppingList.objects.create(**validated_data)
models.py
class ShoppingList(models.Model):
vin = models.CharField(max_length=20)
img_url = models.URLField(blank=True)
year = models.CharField(max_length=20, default="Check Online")
make = models.CharField(max_length=20, default="Check Online")
model = models.CharField(max_length=20, default="Check Online")
grade = models.CharField(max_length=20, default="Check Online")
colour = models.CharField(max_length=20, default="Check Online")
MMR = models.TextField(default="n/a")
check = models.TextField(default="n/a")
run_date = models.CharField(max_length=20, default="Check Online")
timestamp = models.DateTimeField(auto_now=True) # updated timestamp
lane = models.CharField(max_length=10, default="Check Online")
trim = models.CharField(max_length=60, default="Check Online")
mileage = models.CharField(max_length=20, default="Check Online")
human_valuation = models.TextField(default="0")
run_no = models.CharField(max_length=20, default="Check Online")
adesa_id = models.CharField(max_length=20, default="n/a")
engine = models.TextField(default="n/a")
transmission = models.TextField(default="n/a")
wheel_drive = models.CharField(max_length=50, default="n/a")
interior_color = models.CharField(max_length=50, default="n/a")
seller_announcements = models.TextField(default="n/a")
auction_location = models.CharField(max_length=50, default="n/a")
extra = models.TextField(default="n/a")
答案 0 :(得分:1)
您的问题出在.as-console-wrapper { max-height: 100% !important; top: 0; }
行上-这仍然是return lookup
而不是模型实例,而如果您的过滤器没有匹配项,则您将返回单个QuerySet
模型实例。因此,如果该返回值的使用者期望一个模型实例,您将得到这种异常。一种解决方法是返回ShoppingList
,因为您已经确保它不是空的。
您也许还可以使用update_or_create便捷方法-将lookup[0]
放入查询kwargs,将vin=vin, run_date=run_date, check=check
放入validated_data
arg。