Questions tagged [manytomanyfield]

Manytomanyfield is a field of a model class in Django that defines many-to-many relationship

Manytomanyfield is a field of a model class in Django that defines many-to-many relationship.

Quote from docs about manytomanyfield database representation:

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it. Since some databases don’t support table names above a certain length, these table names will be automatically truncated to 64 characters and a uniqueness hash will be used. This means you might see table names like author_books_9cdf4; this is perfectly normal. You can manually provide the name of the join table using the db_table option.

See also:

526 questions
3
votes
1 answer

Spanning multi-valued relationships

I read the django documentation and I found something confusing! https://docs.djangoproject.com/en/3.2/topics/db/queries/#spanning-multi-valued-relationships. It is stated that "When you are filtering an object based on a ManyToManyField or a…
3
votes
2 answers

django-reversion revert ManyToMany fields outside admin

I am using django-reversion in my project. And it works good except one thing: I can't get previous versions of ManyToMany fields. But in django admin it is works, not in my code. To get previous version I use following code: vprod =…
Eugene Nagorny
  • 1,626
  • 3
  • 18
  • 32
3
votes
0 answers

How to override Django many to many field add() and remove() methods

I am trying to set up a Facebook-like activity notification system using Django-Activity-Stream. The library provides a special action signal for creating the actions. According to the documentation, to trigger this action, You can do it through…
3
votes
2 answers

Django Form: Only show manytomany objects from logged in user

The current problem is that my form shows the logged in user all Portfolios ever created. The form should only show portfolios that the logged-in user created. Something like this: associated_portfolios manytomany field =…
3
votes
0 answers

HIbernate - "A Foreign Key referring from has the wrong number of column. Should be 2" error

I've been searching for the solution to this question for a while now. I have found a few threads talking about many-to-many relationships causing this issue, but I don't think that applies to my situation so I'm posting this in a new thread. I…
3
votes
2 answers

Display a comma separated list of ManyToMany items in a Charfield on a ModelForm

I have a model containing a ManyToMany field to a table "Tags". Since this table could be huge, I don't want to display a select in the form, but a coma separated list of tags, provided by a charfield (I suppose). On the saving, I would split the…
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
3
votes
1 answer

Django: No m2m_changed signal when one side of a many-to-many is deleted?

I want to trigger some behaviour whenever a many-to-many relationship changes, but I'm unsure what signal setup is best for capturing changes to that relationship that come about due to the deletion of one side of the relationship. m2m_changed does…
3
votes
3 answers

Determine if an M2M field has a custom through model

With a custom through model for an M2M relationship, .add(), .create() and .remove() are disabled. At the moment, I attempt to use .add() (or whatever) and catch and deal with AttributeError for those custom M2M relationships. Is there an…
Steven
  • 2,658
  • 14
  • 15
3
votes
1 answer

Django: How to limit_choices_to when using custom intermediate table

Let me start by saying that I am working with a legacy database so avoiding the custom intermediate table is not an option. I'm looking for an alternative way to get the limit_choices_to functionality as I need to only present the options flagged by…
3
votes
1 answer

Delete Member of Many To Many Relationship Django Rest Framework

Given the following system # Models class Team(models.Model): name = models.CharField(max_length=128) class Player(models.Model): name = models.CharField(max_length=128) teams = models.ManyToManyField(Team) # Serializers class…
Roger Thomas
  • 822
  • 1
  • 7
  • 17
3
votes
1 answer

django manytomany through

If I have two Models that have a manytomany relationship with a through model, how do I get data from that 'through' table. class Bike(models.Model): nickname = models.CharField(max_length=40) users = models.ManyToManyField(User,…
Joelbitar
  • 3,520
  • 4
  • 28
  • 29
3
votes
2 answers

Django QuerySet with ManyToMany being passed to custom serializer

I've searched all over, but couldn't find a good example. I have two models, one of which has a ManyToManyField. I'm trying to pass a QuerySet that needs information through the ManyToManyField into a custom serializer and return a JSON…
3
votes
1 answer

Django - PermissionRequiredMixin with custom user model as well as AUTHENTICATION_BACKENDS

I am using Django 1.9. When I tried to add PermissionRequiredMixin to my class-based-view, it seems not to work as expected. I created a new user in a auth_group. This auth_group doesn't have any permission to any apps or models. This new user is…
Joey
  • 2,732
  • 11
  • 43
  • 63
3
votes
1 answer

Group by in manytomany fields in Django

Ive got a simple task. There are two models class Song(models.Model): song_title = models.CharField(max_length=200) song_genres = models.ManyToManyField(Genres, null=True, related_name='genres') ... class Genre(models.Model): …
evilmind
  • 111
  • 1
  • 1
  • 5
3
votes
2 answers

django auto-create intermediate model instances for new instance created in foreignkey field model

There are two parts of this question for following situation: I will use example to describe the question: These are my django models: class Zone(models.Model): zone_name = models.CharField(max_length = 10) zone_number =…
Dariusz Krynicki
  • 2,544
  • 1
  • 22
  • 47