Get m2m field (with through model) list in django - django

I'm using Django 2.0.7.
I want to get all the fields of a model. I can get all the normal fields in:
model._meta.fields
And I can get m2m fields in:
model._meta.local_many_to_many
However, if the m2m field has an through table, I can't access them? How can I achieve this?

The m2m fields have a through model that you can access:
YourModel.m2m_field.through._meta.fields

Did you try this ?
model._meta.get_all_field_names()

Related

How to get the name of the Foreign Key tables for a model instance in Django?

I want to get the name of the tables that are foreign key fields for my model instance. For example, I know that I can get data about my fields by using instance._meta.concrete_fields or by getting the name of fields in instance._meta.get_fields but I do not have any idea about getting the foreign keys fields table names. I wonder if you help me.
You can query the _meta (Options) API of the related model for the db_table attribute:
related_field.related_model._meta.db_table
For example:
for field in instance._meta.concrete_fields:
if isinstance(field, models.ForeignKey):
print(field.related_model._meta.db_table)

How to set form field for arrayfield in django model

I have a field by the name related in my model
related=ArrayField(models.IntegerField(),null=True)
I want this field to appear as a choicefield with multiple selection. How do i set the field in my form to achieve this?
You can try with third party multi select field. link

django reverse foreignkey lookup

I am a Django dev and I’m stuck with a problem of reverse foreignkey lookup. The problem is described as follows:
I am working on query optimization. I have a model MicroMessage which has a foreignkey to User (from django.contrib.auth.models) as author. Also there are some other classes which have also foreignkey to User (e.g UserProfile).
I need a query which will fetch author of MicroMessage as well as all users related to any other model from which I can access the UserProfile info of that author without any excessive queries. I tried this:
MicroMessage.objects.select_reverse({'authors':'author_set'})
Please assist me. Thanks in advance.
You're looking for select_related
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
messages = MicroMessage.objects.select_related('author', 'author__userprofile')
The second field (a user's profile) is a reverse foreign key but if implemented as a OneToOneField as the docs suggest, can be queried in the manner shown above if the class was named UserProfile.

Django OneToMany form on related model

I've created a onetomany relationship between two models. The model
that has the foreignkey I can use their formset without problems, but
now I want to fill this relation through the form of the model that
has the "many", I know I can retrieve it using the *_set, but how
can I create a MultipleChoiceField form element of this data?
Thanks for any help!
Regards,
Thiago
Have a look at the docs for fields which handle relationships. You could use a ModelMultipleChoiceField -- it's essentially a MultipleChoiceField that takes its choices from a queryset.

Django represent a many-to-many relationship as a CharField

I have a many-to-many field on one of my models and a ModelForm to represent it. I have it in a template but it shows up as a multiple select field. I need it to show up as a CharField so the user can put in comma-delimited values. Is there any way to do this?
Take a look at the code in django.contrib.admin.widgets.ForeignKeyRawIdWidget to see how the admin's raw_id_fields are implemented and try specifying it as the widgets= kwarg when you define that field on your form.