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
272
votes
3 answers

How to add multiple objects to ManyToMany relationship at once in Django ?

Based on the Django doc, I should be able to pass multiple objects at once to be added to a manytomany relationship but I get a * TypeError: unhashable type: 'list' when I try to pass a django queryset casted in a list. Passing a Queryset or a…
philgo20
  • 6,337
  • 6
  • 34
  • 43
99
votes
5 answers

Direct assignment to the forward side of a many-to-many set is prohibited. Use emails_for_help.set() instead

I am new to Django and didn't find any reference regarding this issue. I am getting this error when i use many to many field in Django model (models.py). I guess the issue is assigning m2m field in view(views.py) from form(forms.py). How to assign…
conf
  • 993
  • 1
  • 6
  • 8
99
votes
13 answers

Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields

I'm trying to modify a M2M field to a ForeignKey field. The command validate shows me no issues and when I run syncdb : ValueError: Cannot alter field xxx into yyy they are not compatible types (you cannot alter to or from M2M fields, or add or…
loar
  • 1,505
  • 1
  • 15
  • 34
37
votes
1 answer

Django Admin ManyToManyField

I've made a model (models.py): class opetest(models.Model): name = models.CharField(max_length=200) author = models.ForeignKey(User, related_name='author') description = models.TextField(u'Test description', help_text = u'Some words…
baobee
  • 433
  • 1
  • 4
  • 8
33
votes
4 answers

Show a ManyToManyField as Checkboxes in Django Admin

Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin? Thanks in advance!
eos87
  • 8,961
  • 12
  • 49
  • 77
22
votes
2 answers

How do I remove multiple objects in a ManyToMany relationship based on a filter?

Given these two Models: class Item(models.Model): timestamp = models.DateTimeField() class Source(models.Model): items = models.ManyToManyField(Item, related_name="sources") I can find all of a Source's Items before a given time using…
bunnyhero
  • 757
  • 2
  • 10
  • 23
22
votes
4 answers

Django: ManyToMany filter matching on ALL items in a list

I have such a Book model: class Book(models.Model): authors = models.ManyToManyField(Author, ...) ... In short: I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single…
iuysal
  • 655
  • 1
  • 5
  • 15
19
votes
2 answers

'QuerySet' object has no attribute ERROR, trying to get related data on ManyToMany fields

i have the following models: class Tag(models.Model): tag_name = models.CharField(max_length=250) tagcat = models.ForeignKey('TagCat') class Subject(models.Model): user = models.ManyToManyField(User) tags =…
mgPePe
  • 5,677
  • 12
  • 52
  • 85
16
votes
1 answer

In Django, how do you retrieve data from extra fields on many-to-many relationships without an explicit query for it?

Given a situation in Django 1.0 where you have extra data on a Many-to-Many relationship: class Player(models.Model): name = models.CharField(max_length=80) class Team(models.Model): name = models.CharField(max_length=40) players =…
iammichael
  • 9,477
  • 3
  • 32
  • 42
16
votes
3 answers

Instantiate model instance with manytomany field in Django

I have a method that works, but it seems very clumsy, and I would think there is a better way to do this. I have a Model that relates a user on my site (a twitter clone for learning purposes) to a list of other users. Right now when I create a new…
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
14
votes
1 answer

Django syncdb conflicting related_name when using inheritance and ForeignKey

This time I think it's not me being stupid but an actual conflict. I have the below code (simplified): from django.db import models class Alpha(models.Model): relation = models.ForeignKey('Delta', related_name = 'reverse_relation', blank =…
Mark
  • 18,730
  • 7
  • 107
  • 130
14
votes
4 answers

Displaying both sides of a ManyToMany relationship in Django admin

Say I have the following models that have a many-to-many relationship: models.py: class Foo(models.Model): name = models.TextField() class Bar(models.Model): name = models.TextField() foos = models.ManyToManyField(Foo,…
Nobilis
  • 7,310
  • 1
  • 33
  • 67
14
votes
4 answers

How can I sort by the id of a ManyToManyField in Django?

I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the…
Tim Trueman
  • 1,513
  • 3
  • 17
  • 28
13
votes
5 answers

FieldDoesNotExist: ManyToManyField has no field named None

I have two models in Django 1.8.8: class Company(models.Model): name = models.CharField(max_length=200) members = models.ManyToManyField(User) class Folder(models.Model): name = models.CharField(max_length=200) slug =…
citos88
  • 141
  • 6
13
votes
4 answers

How to add data to ManyToMany field using Django Rest Framework?

I'm relatively new to DJango and I am looking to add data to a many-to-many field using serializers from rest framework. My Model: class IngFamily(models.Model): name=models.CharField(max_length=30, unique=True, verbose_name='ingredient…
1
2 3
35 36