Accessing session in Django models?

23 views
Skip to first unread message

Neto

unread,
Aug 3, 2016, 4:25:25 PM8/3/16
to Django users
I have a field with limit_choices_to, and want it to be only filtered items related the account ('ID') user. How can I do this?

Example (Does not work):

class Car(models.Model):
user = models.ForeignKey(User)
color = models.CharField(max_length=200)
typeo = models.ForeignKey(Typeo, limit_choices_to={'user__id': session['user_id']})

Constantine Covtushenko

unread,
Aug 3, 2016, 6:39:10 PM8/3/16
to django...@googlegroups.com
Hi Neto,

As said in Documentation of 'limit_choices_to'  value for this key should be: 
`Either a dictionary, a Q object, or a callable returning a dictionary or Q object can be used.`

For your case these mean that you should use at least callable as 'use__id' should be defined every Request/Response cycle.

Regards,


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8d2b4b10-83b9-489c-b834-31ddebc0c9df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages