FAQ Database Discussion Community


Did I break my DB while changing ManyToMany field in Django?

python,django,postgresql,manytomanyfield
So I had a ForeignKey field and I needed to convert it to a ManyToManyField. When I ran the migration it gave this error: ValueError: Cannot alter field core_app.IndieTrack.contributors into core_app.IndieTrack.contributors - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through=...

Django ManytoMany retireving all objects that fulfill a condition simoultaneously

django,orm,manytomanyfield
Using the Django models examples on querymaking (https://docs.djangoproject.com/en/1.8/topics/db/queries/): How can I select all the entries that have ALL of it's author's names starting with R using the filter function?...

Using the tables defined in the same models.py for list_display and populating the manyToMany fields of that table in admin forms

django,django-models,django-forms,django-admin,manytomanyfield
I am using Django 1.7, python 3.4 one of my models contains one class named Enterprise along with five other classes(Type, Products, etc.) each of which have a ManyToMany relation with Enterprise. class Enterprise(models.Model): enterprise = models.CharField(max_length=255) slug = models.SlugField(max_length=255, unique=True) is_active = models.BooleanField(default=True) types = models.ManyToManyField(Type) products = models.ManyToManyField(Product)...

Search a Many to Many Relationship

django,django-models,many-to-many,manytomanyfield
Given a Contributor, I'm trying to find the Registrations the Contributor belongs to. I can't figure out how to traverse the M2M- I need to know which Registrations contain the Contributor in their 'conrtibutor' field...? class Registration(TimeStampedModel): dataset = models.ForeignKey('Dataset', related_name='registration') contributor = models.ManyToManyField('Contributor', related_name='registrations') # many contributors may be...

How to add an entry to ManyToManyField in Django?

django,django-models,many-to-many,manytomanyfield,junction-table
I have the following models: class Vote(models.Model): cmt = models.ForeignKey(Comment) user = models.ForeignKey(User) vote_type = models.SmallIntegerField(default=0) # vote_type is +1 for upvote & -1 for downvote class Comment(models.Model): uuid = models.CharField(max_length=40, default='random') user = models.OneToOneField(User, primary_key=True) # ForeignKey('User') text = models.CharField(max_length=300, null=True) votes = models.ManyToManyField(User, through='Vote', related_name='votes_table') If I wasn't...

Django ValueError when trying to save ManyToMany Values from a Form

django,django-models,django-forms,manytomanyfield
I get the error "" needs to have a value for field "dataset" before this many-to-many relationship can be used." when trying to assign values to a ManyToMany field in my views. I've looked at many related questions here on SO that say I must save my Dataset object first....

Django ManyToManyField and values from other class

django,manytomanyfield
I have a lot of standard services with different prices in different ServicePlaces. My models.py: class Service(models.Model): service = models.CharField(max_length=200, unique=True) class ServicePlace(models.Model): place = models.CharField(max_length=200, unique=True) #what services are provided in this ServicePlace: service = models.ManyToManyField('Service') class Price(models.Model): price = models.DecimalField(max_digits=6, decimal_places=2) service = models.ForeignKey("Service") place = models.ForeignKey("Place") How...

Django ManyToMany reverse query with specific key

django,postgresql,manytomanyfield
I have two tables. The first, is a basic user table. And the second is a table called Section. def User(models.Model): ... is_teacher = models.BooleanField(default=False) ... def Section(models.Model): ... teachers = models.ManyToManyField(User, related_name="teachers") students = models.ManyToManyField(User, related_name="students") is_active = models.BooleanField(default=True) ... I would like to get all the student users...

Category slug in article URL (ManyToManyField)

django,url,categories,slug,manytomanyfield
I am trying to build a URL pattern such as /category/article/ but the problem is, one article can be related to many categories, so the URL should be something like /category+category/article/ What I have: models.py from django.db import models from django.core.urlresolvers import reverse class Category(models.Model): name = models.CharField(max_length=128, unique=True) slug...