As mentioned in the comments, you have no field in your Question
model named user
. The field you do have that's close (users
) is not defined properly. It looks as though you want a ForeignKey
field or possibly a ManyToMany
field depending on how you want to structure that relationship. You should check out the docs on relationships and how they are handled in the django ORM. Also, your static method update_users
doesn't seem to do much. It looks like you want to add all users to a particular question, but you are passing a list of values by calling the values_list
method:
userz = User.objects.values_list('id','username')
Question.objects.all().update(users=userz)
but the update method of your model could just take the objects themselves without going the further step of making a values_list
. I am not sure why you would do this but just as an example of what kind of argument the update method expects when dealing with related objects:
Question.objects.all().update(users=Users.objects.all())
Using the values_list would be an error I believe. I strongly recommend checking out the Django docs to brush up on ORM usage.
As an aside... you don't need to do this:
def __str__(self):
return "{question_text}".format(question_text=self.question_text)
As you can see in your definition of the question_text
field, it is a string field (CharField
) so you can just do:
def __str__(self):
return self.question_text
Edit 1:
Django uses class variables to define fields, but the meta-programming magic used to make it work requires you to use their predefined field classes so it can dynamically populate the fields from the database. You defined that field by using a static list:
users = User.objects.values_list('id','username')
This definition does not allow it to fetch data dynamically from the database, but more importantly, when creating the table which is represented by your model definition, it doesn't properly define that column. Each field represents a column in your db and if you do not define it by using the proper field classes provided in the django.models module it will not create the field.
This is all covered in the starter docs.