Foreignkey dropdown list for EditView DRF #9194
Unanswered
spinxi
asked this question in
Question & Answer
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I have this models:
I'm Building DRF API.
Now I'm currently making Edit view for this Loads, I created serializers, views , and everything works well, but there is only 1 problem, I really can't decide how can I get users dropdown list on front for this LoadDrivers field:
driver = models.ForeignKey(User, default=None, null=True, blank=True, on_delete=models.DO_NOTHING, limit_choices_to={'is_driver': True})
For example when I go to edit view and I wanna change driver, how can I get that users dropdown list?
One way to do this, I think that would be to create separate API endpoint that returns all Users and then do some stuff with Javascript.
without DRF, with just forms and django templates, I would have {{ form }} and for driver field it would render with users dropdown list where 'is_driver': True as I mentioned it in LoadDriver model.
So this is my main problem.
There are also trailer, truck fields from Loads model, and there I have also the same problem, I have to get these lists, if I use Django templates, on EditView it generate like this:
So I need to get the same results with API, what is the best practice?
Beta Was this translation helpful? Give feedback.
All reactions