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
5
votes
1 answer

Get all objects defined by a Django ManyToManyField relationship

The site I'm building allows users to create "posts" and has a Twitter-like concept of followed users. For a given user, I'd like to show all of the posts from the users that they follow. Here are my simplified models: class User # a standard…
mitchf
  • 3,697
  • 4
  • 26
  • 29
5
votes
5 answers

How to make recursive ManyToManyField relationships that have extra fields symmetrical in Django?

class Food_Tag(models.Model): name = models.CharField(max_length=200) related_tags = models.ManyToManyField('self', blank=True, symmetrical=False, through='Tag_Relation') def __unicode__(self): return self.name class…
kliao
  • 549
  • 1
  • 6
  • 14
5
votes
1 answer

Test Django ModelForm which depends on model with manytomanyfield

I am trying to unit test one of my Django forms, which I use in the admin of a Django-based app. The Meta class of that form has a model field. Not sure about what approach is better: Instantiate a model, and the build the ModelForm using…
5
votes
1 answer

How to write migration to change primary key of model with ManyToManyField

I have a UserProfile model that refers to my User model with a OneToOneField. I also use the post_save signal to auto create the UserProfile when the user is created. This works great apart from when creating a user through the admin (where I use…
Hamish Downer
  • 16,603
  • 16
  • 90
  • 84
5
votes
1 answer

Django REST Errors when serializing model with ManyToManyField

I've created a class modeling a group of files within a software product build, on a Django server using the Django-REST package. The design is that the group of files (a Depot instance) should be able to be assigned to multiple Build instances…
4
votes
2 answers

How to select all objects that aren't in a manytomany relation in Django

I have the following models (resumed) in an application: class Account(models.Model): name = models.CharField(max_length=64) plans = models.ManyToManyField('Plan') extra_services = models.ManyToManyField('Service') class…
lmlf
  • 111
  • 4
4
votes
4 answers

Django ModelForm fails to save "many to many" records using ModelForm and save_m2m

I am new to Django and using this project to learn it. I am able to save the Journal record but the many to many relationship does not work. This 'create' view displays the correct form including the multi-select box with all of the cryptos listed…
DBTales
  • 91
  • 8
4
votes
1 answer

Django m2m_changed pk_set is empty

Django version 1.10.7, I have a problem where sometimes my receiving m2m_changed signal on a ManyToMany field has pk_set empty. The model m2m part (note it's a self reference): class Profile(TimeStampedModel): following =…
jaywink
  • 280
  • 3
  • 14
4
votes
2 answers

Django select objects with empty ManyToManyField

Considering the following models, knowing a family, how do I select Kids with no buyers? class Family... class Kid(models.Model): name = models.CharField(max_length=255) family = models.ForeignKey(Family) buyer =…
fmalina
  • 6,120
  • 4
  • 37
  • 47
4
votes
3 answers

How can I store history of ManyToManyField using django-simple-history.

How can I store history of ManyToManyField using django-simple-history. I used HistoricalRecords with attribute m2m_filds but it is throwing error: unexpected keyword argument 'm2m_fields'
4
votes
2 answers

Django self-recursive ManyToManyField filter query

I have a model like so: class Activity(models.Model): title = models.CharField(max_length=200) summary = models.TextField(null=True, blank=True) tasks = models.ManyToManyField('self', symmetrical=False, null=True, blank=True) It…
ninja123
  • 1,069
  • 1
  • 13
  • 21
4
votes
2 answers

DRF - post to ManyToMany field

In my django app: models.py: class Destination(models.Model): name=models.CharField(max_length=30) class Ride(models.Model): driver = models.ForeignKey('auth.User', related_name='rides_as_driver') …
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
4
votes
1 answer

django-rest-framework ManyToManyField create and update

I'm new in Django, so I have a some problems. I'm using django-rest-framework. These are my model classes: class Product(models.Model): name = models.CharField(max_length=100) price = models.FloatField() sizes =…
fran
  • 1,119
  • 1
  • 10
  • 27
3
votes
1 answer

How to use save_model in an AdminForm containing a M2M field?

i'm having an annoying issue with django model system + its default admin. Let's assume i have a very simple model like: class Note(models.Model): text = models.CharField(max_length=200) def __unicode__(self): return self.text and a…
Valerio
  • 2,390
  • 3
  • 24
  • 34
3
votes
1 answer

How to add custom field in manytomany through table in Django

I have a model which has manytomany relation with another model. This creates a hidden through table with two foreign key field of previous two tables. Now I want to add custom field in addition to the existing two foreign key field in the through…
Nahidujjaman Hridoy
  • 1,175
  • 12
  • 28