many-to-many

Add an object by id in a ManyToMany relation in Django

Add an object by id in a ManyToMany relation in Django Question: Django’s ManyToMany field can be populated using my_field.add(my_instance), but as I understand it only my_instance.id is actually needed to perform the corresponding SQL query. If I want to add an object by its id, I can use my_field.add(MyModel.objects.get(id=id)), but this will generate two …

Total answers: 1

Proper way to bulk_create for ManyToMany field, Django?

Proper way to bulk_create for ManyToMany field, Django? Question: I have this code for table populating. def add_tags(count): print “Add tags” insert_list = [] photo_pk_lower_bound = Photo.objects.all().order_by(“id”)[0].pk photo_pk_upper_bound = Photo.objects.all().order_by(“-id”)[0].pk for i in range(count): t = Tag( tag = ‘tag’ + str(i) ) insert_list.append(t) Tag.objects.bulk_create(insert_list) for i in range(count): random_photo_pk = randint(photo_pk_lower_bound, photo_pk_upper_bound) p = …

Total answers: 2

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django? Question: I’m having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey? Asked By: user3915903 || Source Answers: Well, there’s essentially two questions here: What is the …

Total answers: 2

How to query directly the table created by Django for a ManyToMany relation?

How to query directly the table created by Django for a ManyToMany relation? Question: I have a model MyModel2 with a ManyToManyField related to another model MyModel1. How can I get the pairs mymodel1.id, mymodel2.id, as represented in the table Django creates for this relation? Do I have to do a raw SQL query on …

Total answers: 1

SqlAlchemy and Flask, how to query many-to-many relationship

SqlAlchemy and Flask, how to query many-to-many relationship Question: I need help creating SqlAlchemy query. I’m doing a Flask project where I’m using SqlAlchemy. I have created 3 tables: Restaurant, Dish and restaurant_dish in my models.py file. restaurant_dish = db.Table(‘restaurant_dish’, db.Column(‘dish_id’, db.Integer, db.ForeignKey(‘dish.id’)), db.Column(‘restaurant_id’, db.Integer, db.ForeignKey(‘restaurant.id’)) ) class Restaurant(db.Model): id = db.Column(db.Integer, primary_key = True) …

Total answers: 1

How to add column in ManyToMany Table (Django)

How to add column in ManyToMany Table (Django) Question: From the example of Django Book, I understand if I create models as following: from xxx import B class A(models.Model): b = ManyToManyField(B) The Django would create a new table(A_B) beyond Table A, which has three columns: id a_id b_id But now I want to add …

Total answers: 4

Django Many-to-Many (m2m) Relation to same model

Django Many-to-Many (m2m) Relation to same model Question: I’d like to create a many-to-many relationship from and to a user class object. I have something like this: class MyUser(models.Model): … blocked_users = models.ManyToManyField(MyUser, blank=True, null=True) The question is if I can use the class reference inside itself. Or do I have to use “self” insead …

Total answers: 6

Django ManyToMany relationship

Django ManyToMany relationship Question: In models.py: from django.db import models from django.utils.translation import ugettext as _ # Create your models here. class Category(models.Model): name = models.CharField(_(u"Name"), max_length=250) products = models.ManyToManyField("Product", verbose_name=_(u"Products"), blank=True, null=True, related_name="+") class Product(models.Model): name = models.CharField(_(u"Name"), max_length=250) category = models.ManyToManyField("Category", verbose_name=_(u"Category"), blank=True, null=True, related_name="+") In admin page: The question: How can the …

Total answers: 2

How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?

How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute? Question: I’m trying to implement a self-referential many-to-many relationship using declarative on SQLAlchemy. The relationship represents friendship between two users. Online I’ve found (both in the documentation and Google) how to make a self-referential m2m relationship where …

Total answers: 2

SQLAlchemy ManyToMany secondary table with additional fields

SQLAlchemy ManyToMany secondary table with additional fields Question: I have 3 tables: User, Community, community_members (for relationship many2many of users and community). I create this tables using Flask-SQLAlchemy: community_members = db.Table(‘community_members’, db.Column(‘user_id’, db.Integer, db.ForeignKey(‘user.id’)), db.Column(‘community_id’, db.Integer, db.ForeignKey(‘community.id’)), ) class Community(db.Model): __tablename__ = ‘community’ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) members = db.relationship(User, …

Total answers: 2