How to set User-based form field choices in a django ModelForm (with django-allauth) -
i'm using django 1.8 , django-allauth. have simple form dropdown menu allows users choose character model. have usercharacter model looks this:
class usercharacter(models.model): user = models.foreignkey(user) character = models.foreignkey(character) is_default = models.booleanfield(default=false)
in main form i'm displaying "character" field, appears dropdown menu of character objects. far, - users can select character , saved usercharacter association.
what i'm trying display character objects don't have usercharacter linked logged in user. use limit_choices_to feature, so:
character = models.foreignkey(character, limit_choices_to={'id__in': usercharacter.objects.filter(user_id=[user_id]))
my problem is, don't know how access current user object usercharacter model or modelform. need access current request @ django-allauth logged in user object. in place of [user_id] put
self.request.user.id
to current user's id django-allauth, can't model here. how go changing form choices in modelform based on current user object? much!
since needs logged in user have in view
override form characters queryset
based on user.
form.character.queryset = character.objects.filter(....)
you can pass request.user
form , filter there in constructor.
Comments
Post a Comment