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

Django manytomany field, how to get check for 80% subset/ match?

I have a Django db with cooking recipes. I want to query all users who have at least 80% ingredients to make a recipe. How do I achieve this? Or how do I query for users who are missing only 1 ingredient for the recipe? models.py class…
Deepti
  • 576
  • 2
  • 5
  • 15
7
votes
2 answers

django manytomanyfield .add() method

Suppose i have : class Album(models.Model): photos = models.ManyToManyField('myapp.Photo') photo_count = models.IntegerField(default = 0) class Photo(models.Model): photo = models.ImageField(upload_to = 'blahblah') What is want is,…
Santana
  • 177
  • 3
  • 9
6
votes
2 answers

Django: Saving multiple ManyToMany fields within a transaction

this is the representation of my models: class B(models.Model): """I'm a dummy model, so doesn't pay atention of what I do""" name = models.CharField(max_length=250) class A(models.Model): name = models.CharField(max_length=250) many_b…
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
6
votes
1 answer

Duplicate django objects with ManyToManyFields

I use Django and I have some objects with ManyToManyFields. I'd like to duplicate these objects. I've found 'deepcopy' which works almost perfectly. >>> e = Equipement.objects.get(pk=568) >>> ee = deepcopy(e) >>> ee.connexion.all() [
psychoze
  • 63
  • 4
6
votes
1 answer

Django throws 'Direct assignment to the forward side of a many-to-many set is prohibited.' error

I have two models in Django Users and Contexts.I have defined the models as below class User(models.Model): userId = models.PositiveIntegerField(null = False) pic = models.ImageField(upload_to=getUserImagePath,null=True) Email =…
Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
6
votes
1 answer

Django: ModelMultipleChoiceField won't save data

My modelform has a field with a ModelMultipleChoiceField that has a queryset to a Category object. The problem is the form submits, but the Category data doesn't save. Thoughts? Many thanks! Form: class MealForm(forms.ModelForm): class Meta: …
Emile
  • 3,464
  • 8
  • 46
  • 77
6
votes
2 answers

Django: InlineAdmin and ManyToManyField with 'through'

I'm having a simple Gallery model, that is related to an Image model via a many-to-many relationship through a table that has an ordering-attribute: # models.py class Image(models.Model): .... class Gallery(models.Model): images =…
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
6
votes
2 answers

Django - ManyToManyField in a model, setting it to null?

I have a django model (A) which has a ManyToManyField (types) to another model (B). Conceptually the field in A is an 'optionally limit this object to these values'. I have set blank=null and null=True on the ManyToManyField. I have created an…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
6
votes
3 answers

Can I add a manager to a manytomany relationship?

I have two models that has a manytomany relationship with a 'through' table in some way? class Bike(models.Model): nickname = models.CharField(max_length=40) users = models.ManyToManyField(User, through='bike.BikeUser') The BikeUser…
Joelbitar
  • 3,520
  • 4
  • 28
  • 29
6
votes
3 answers

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file =…
saturdayplace
  • 8,370
  • 8
  • 35
  • 39
6
votes
1 answer

Django testing: DatabaseError: no such table for ManyToManyField

I've written a couple of tests for really simple blog app, but the many to many relationship fails when I run the test: ./manage.py test myblog DatabaseError: no such table: myblog_post_tag Yet when I do ./manage.py sql myblog: BEGIN; CREATE TABLE…
binarymac
  • 61
  • 2
5
votes
2 answers

ManyToManyField widget in a django admin change list?

In the change list of the django admin, I want to use list_editable to display a django-autocomplete widget for a ManyToManyField. I found something similar here: list_editable and widgets Normally including a ManyToManyField in list_display raises…
kwnd
  • 101
  • 1
  • 5
5
votes
1 answer

Django: How to Properly Use ManyToManyField with Factory Boy Factories & Serializers?

The Problem I am using a model class Event that contains an optional ManyToManyField to another model class, User (different events can have different users), with a factory class EventFactory (using the Factory Boy library) with a serializer…
gpsugy
  • 1,199
  • 1
  • 11
  • 25
5
votes
3 answers

Django: AJAX ManyToManyField in admin

I want to display ManyToManyFields in admin just like filter_horizontal does, but populate the options as the user types into the filter field. There are many options and loading them all at once takes a lot of time. I found…
onurmatik
  • 5,105
  • 7
  • 42
  • 67
5
votes
2 answers

count values from manytomanyfield

I am trying to count the distinct values from a group of objects that have a manytomanyfield e.g. object article has manytomanyfield of tag objects one article has tags "tag1" "tag2" another article has tags "tag2" "tag3" I would like to figure…
DantheMan
  • 7,247
  • 10
  • 33
  • 36
1 2
3
35 36