Amon
Amon

Reputation: 2951

"Query set has no attribute <field>" but I'm not doing any field lookup

In Django I'm using filter to return a Queryset so I can update it in my serializer. The problem is I get getting this error: 'QuerySet' object has no attribute 'vin'. I understand the error occurs when you try to lookup a field on a Queryset, in which case you would use get() instead of filter(), but I'm not doing a field lookup. And my database does get updated as intended, but I receive the error regardless. The docs use it the same way: # Update all the headlines with pub_date in 2007. Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same'). But I'm not sure why it's acting as if I'm still trying to do a field lookup on the queryset

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")

Upvotes: 0

Views: 37

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

Your problem is with the return lookup line - that's still a QuerySet rather than a model instance, whereas if your filter gets no matches you're returning a single ShoppingList model instance. So if consumers of that return value expect a model instance, you'll get this kind of exception. One fix is to return lookup[0] instead, since you've already ensured that it's non-empty.

You might also be able to use the update_or_create convenience method - put vin=vin, run_date=run_date, check=check in the query kwargs and validated_data in the defaults arg.

Upvotes: 1

Related Questions