Attribute error when attempting to get a value for field

2024/7/7 7:33:53

I'm working with the django rest framework and the serializer I'm trying to use is creating errors. I'm trying to do something like https://gist.github.com/anonymous/7463dce5b0bfcf9b6767 but I still get the error. the models are

class Visitor(models.Model):user = models.OneToOneField(User)
check_ins = models.IntegerField(default=0)@classmethod
def create(cls, username, email, password):user = User.objects.create_user(username, email, password)visitor = cls(user=user)visitor.save()return visitordef __str__(self):return self.user.username

and the default user class and the serializers are

class UserSerializer(serializers.ModelSerializer):class Meta:model=Userfields = ('username')class VisitorSerializer(serializers.ModelSerializer):user = UserSerializer()class Meta:model=Visitorfields = ('id','check_ins','user')

I get this error

Got AttributeError when attempting to get a value for field user on serializer VisitorSerializer. The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance. Original exception text was: 'QuerySet' object has no attribute 'user'.

Answer

The issue is that you are passing a queryset into your serializer without setting the many flag. The error is telling you that the serializer is trying to access queryset.user when it should be accessing visitor.user, so you need to tell the serializer that there are multiple objects (instead of a single one) by passing many=True.

https://en.xdnf.cn/q/73349.html

Related Q&A

Why did I have problems with alembic migrations

Project structue(only directory with DB migrations):--db_manage:alembic.ini--alembic:env.pyscript.py.makoREADME--versions:#migration filesWhen I try to run command: python db_manage/alembic/env.py, I h…

Python and App Engine project structure

I am relatively new to python and app engine, and I just finished my first project. It consists of several *.py files (usually py file for every page on the site) and respectively temple files for each…

Text object in matplotlib doesnt respond to zooming properly

. Hello, everyone.I recently try to add text object in my plot. But when I zoom into the text, the text size remains unchanged. What I want to have is that the text size will increase when I zoom in an…

Python unit testing code which calls OS/Module level python functions

I have a python module/script which does a few of theseAt various nested levels inside the script I take command line inputs, validate them, apply sensible defaults I also check if a few directories ex…

How do I connect/disconnect/configure a wireless network in python?

Im looking to see if there is a way to connect or disconnect to a wireless network in python, preferably a way that would work for both public and secured networks if I supplied the password. If I can …

CSRF protection on AJAX authentication in Flask

Id like to AJAXify both a login and a signup form on a site. Up to now Ive been using WTForms mainly for its built-in CSRF protetion, but for this project I didnt feel like it was worth it -- an extra …

Pandas groupby and Multiindex

Is there any opportunity in pandas to groupby data by MultiIndex? By this i mean passing to groupby function not only keys but keys and values to predefine dataframe columns?a = np.array([foo, foo,…

How to dump YAML with explicit references?

Recursive references work great in ruamel.yaml or pyyaml: $ ruamel.yaml.dump(ruamel.yaml.load(&A [ *A ])) &id001 - *id001However it (obviously) does not work on normal references: $ ruamel.yaml…

How to set a Pydantic field value depending on other fields

from pydantic import BaseModelclass Grafana(BaseModel):user: strpassword: strhost: strport: strapi_key: str | None = NoneGRAFANA_URL = f"http://{user}:{password}@{host}:{port}"API_DATASOURCES…

Cascade multiple RNN models for N-dimensional output

Im having some difficulty with chaining together two models in an unusual way. I am trying to replicate the following flowchart:For clarity, at each timestep of Model[0] I am attempting to generate an …