Choice field model django - django

I have created a model called ingrident where I have store all the ingrident name now I want to pass all these items into choice of a field in another model.
How can I do this I don't want to use foreign key because I want to Crete the choice field in inline formset so that the user can Crete recipe by selecting multiple ingrident and giving each ingrident with quantity

Related

How to restrict a list of fields for a django model generated using _meta.fields by use of a filter

In my project I am trying to retrieve field names of a model which has been selected using a dropdown list. The idea is to list all underlying fields of the selected model, select one of the fields and save the combination in a model for use in a scenario later.
To generate the list of fields I am using:
model_fields = [fld.name for fld in modelSelected._meta.fields]
where modelSelected is the previously selected model (as sent from template using an Ajax function).
The set up is working as intended with one caveat. When I start to type in the input field for model field name (using jQuery autocomplete), all the fields of the model are shown in the (selection) list.
Is there a way to restrict the field names in what I am attempting to do here?
For example, to accomplish an autocomplete on a model HoldingCoy I would be doing:
parent_coy = HoldingCoy.objects.filter(name__icontains=q).annotate(value=F('name'),
label=F('name')).values('holding_coy', 'value', 'label')
How can I apply filter to restrict number of fields displayed in the drop down list and achieve a similar effect as used for querying a model instance (as in the above example)?

How can I restrict the options for ForeignKey to keys inside a ManyToMany field

I have a ManyToManyField that stores a users "inventory" which is a ManyToManyField of items, I also have a foreign key field "active" that stores the id of the active item of the user. I was wondering if there was a constraint I could put so that any "active" id must be in the ManyToMany inventory.
You could override the save() method of the model and catch and handle possible non-compliance there.
https://docs.djangoproject.com/en/4.0/topics/db/models/#overriding-model-methods

How to restrict a child table's search field value on pre-selected parent field value using jQuery+Ajax

I am trying to implement jQuery autocomplete search for a model field based on a related model's FK field. For instance, names of a child model's values to be restricted on the preselected value of the key field of parent model (akin to Country > City scenario).
My question is:
How do I pass the Country model's primary key (id) (selected in the country autocomplete field) to the CitySearch view (variable country_search_id) which I could then use in the QuerySet (in the first .filter)? The country id value is stored in a form field on the page.
This is not optimal at all, but might work:
You could store the country_id captured by the onChange's Ajax function on a hidden text input, then retrieve it on your City Search's view.

Django User Model with ManyToMany relationship, how to update/ create new profile with relationship?

I have a class that has a one to one relationship with my User class. In this class (called UserLocations), I have a field that is a ManyToManyField on another class (we will call this model Locations).
In my Locations class, I have a field called 'abbreviation' which is a four character abbreviation of the location name. I have a front end website where an admin can create a new user or update an existing users locations (based on the abbreviation field).
When I am going to create a new user, how would I set/ update that ManyToMany field based on the 'abbreviation' field? It is a list of abbreviations that tie to that 'abbreviation' field in the Locations model.
# Assuming you get the chosen abbreviation in a variable called location_abbreviation:
desired_location = Location.objects.get(abbreviation=location_abbreviation)
user = User.objects.create(...)
user_location = UserLocation.objects.create(user=user, ...)
user_location.locations = [desired_location]
user_location.save()
Let me know if I misunderstood your question.

Django foreign key with different model field values

I want to create a foreign key relationship in a model, where it actually provides a select dropdown for the model form based on another field value of the model. Normally dropdown shows the values of __str__ methods return value. I want to keep this also since it used in other places.
For example, consider two models Order and Item.
Item have fields called item_code and item_name.
__str__ will return item_name.
Order model will have foreign key to Item model. But I want it provides the item_code field values in the dropdown for item selection.
How can I achieve this without changing general foreign key behavior?
You would do this in the form, by defining a subclass of ModelChoiceField with a custom label_from_instance method, as described in the documentation.