0

I'm having Model Question, which contains all user list field. After creating new user, users field is not updating and I need to re-run server to see changes and i dont want to do that.

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    users = User.objects.values_list('id','username')
    authorized = MultiSelectField(choices=users,null=True)
    def __str__(self):
        return "{question_text}".format(question_text=self.question_text)

@staticmethod
def update_users(sender, instance, **kwargs):
        #q = Question.objects.all()
        userz = User.objects.values_list('id','username')
        Question.objects.all().update(users=userz)

post_save.connect(Question.update_users, sender=User)

I'm geting : Question has no field named 'users'

I followed those tips : How can I have a Django signal call a model method? and still not working and im stuck. Thanks for help!

adampicker
  • 29
  • 1
  • 1
  • 6
  • 1
    Read ther error, your model doesn/t have any field named `user`. – Astik Anand Jun 02 '18 at 11:17
  • 1
    Moreover, I don't get why you have written `users = User.objects.values_list('id','username')` in Question Model. – Astik Anand Jun 02 '18 at 11:17
  • @AstikAnand its my mistake, missclick, and i need that field for my Multiselect widget. And my problem is that my 'choices' are not updating after another user register – adampicker Jun 02 '18 at 11:46

1 Answers1

0

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.

Verbal_Kint
  • 1,366
  • 3
  • 19
  • 35
  • im using 'values_list' because passing 'usesr= User.objects.all()' to choices return : 'TypeError: 'User' object is not iterable' – adampicker Jun 02 '18 at 12:00
  • @adampicker That doesn't make much sense, `DjangoObject.objects.all()` returns an iterable object, it looks like you passed in a single `User` object based on the error message provided. But I would recommend going over the starter project in the django docs or at least the relationships section because `values_list` should not work when filtering/updating on a related field (unless it is a list of id numbers, but I'm not even sure that works in the newer versions of django). – Verbal_Kint Jun 02 '18 at 12:06
  • @adampicker based on your code above and your comment here you would benefit greatly from reading through the django docs. It clearly states how these fields work there as opposed to trying different things based on error messages. – Verbal_Kint Jun 02 '18 at 12:09
  • all right, but my problem is that im getting that FieldDoesNotExist (users filed in Question model) while field it is there. – adampicker Jun 02 '18 at 12:18
  • MultiSelectField ass choices accept list of tuples that's why values_list is there, – adampicker Jun 02 '18 at 12:19
  • @adampicker As stated in my answer, the field is not properly defined. Check my edit above. – Verbal_Kint Jun 02 '18 at 12:36
  • @adampicker regardless of what MultiSelectField expects, users is not properly defined, There is no column called users in your database. – Verbal_Kint Jun 02 '18 at 12:40