m2m

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

落花浮王杯 提交于 2020-01-13 18:04:14
问题 class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

北慕城南 提交于 2020-01-13 18:02:04
问题 class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id

ManyToMany field not saved when using Django admin

心不动则不痛 提交于 2020-01-02 01:02:10
问题 I'm experiencing a weird problem which I hope someone in here may be able to shed some light on. I'm overriding the save() method of a model to add some values to a ManyToMany-field after running super(). My problem is that when I'm saving in Django admin the values seems to get added to the relationship but is then empty again. If however I do it from manage.py shell it works without problem. I've put two print statements in there and they produce the exact same output regardless of if I'm

pythonのsqlalchemy多对多关系

我与影子孤独终老i 提交于 2019-12-31 16:07:37
现在来设计一个能描述“图书”与“作者”的关系的表结构,需求是 一本书可以有好几个作者一起出版 一个作者可以写好几本书 1 #!/usr/bin/env python 2 from sqlalchemy import Table,Column,Integer,String,DATE,ForeignKey 3 from sqlalchemy.orm import relationship 4 from sqlalchemy.ext.declarative import declarative_base 5 from sqlalchemy import create_engine 6 from sqlalchemy.orm import sessionmaker 7 8 9 engine = create_engine("mysql+pymysql://root:root@localhost/testuser?charset=utf8",encoding="utf-8",echo=True) 10 # 生成orm基类 11 Base = declarative_base() 12 13 14 # 创建表映射 15 book_m2m_author = Table("book_m2m_author",Base.metadata, 16 Column("book_id",Integer

Reverse relations with django-gm2m using “through” relation

跟風遠走 提交于 2019-12-25 01:45:42
问题 I don't understand how to follow many-to-many relations in the reverse direction with django-gm2m. Here is an example of an models.py: from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey from gm2m import GM2MField class A(models.Model): pass class B(models.Model): pass class X(models.Model): things = GM2MField() class Y(models.Model): things = GM2MField(through='Yrel') class Yrel(models.Model)

Django: accessing ManyToManyField objects after the save

你说的曾经没有我的故事 提交于 2019-12-21 06:05:16
问题 This is baffling me... When I save my model, the book objects are unchanged. But if I open the invoice and save it again, the changes are made. What am I doing wrong? class Invoice(models.Model): ... books = models.ManyToManyField(Book,blank=True,null=True) ... def save(self, *args, **kwargs): super(Invoice, self).save(*args, **kwargs) for book in self.books.all(): book.quantity -= 1 if book.quantity == 0: book.sold = True; book.save() Edit: I've tried using the post_save signal, but it works

Display m2m field defined via 'through' in admin

廉价感情. 提交于 2019-12-20 23:26:05
问题 I have the following model classes: class Category(models.Model): category = models.CharField('category', max_length=200, blank=False) class Book(models.Model): title = models.CharField('title', max_length=200, blank=False) categories = models.ManyToManyField(Category, blank=False, through='Book_Category') class Book_Category(models.Model): book = models.ForeignKey(Book) category = models.ForeignKey(Category) When adding a new book object in admin interface I would like to also add a new

Django signal m2m_changed not triggered

↘锁芯ラ 提交于 2019-12-19 05:20:53
问题 I recently started to use signals in my Django project (v. 1.3) and they all work fine except that I just can't figure out why the m2m_changed signal never gets triggered on my model. The Section instance is edited by adding/deleting PageChild inline instances on an django admin form. I tried to register the callback function either way as described in the documentation, but don't get any result. Excerpt from my models.py from django.db import models from django.db.models.signals import m2m

Django signal m2m_changed not triggered

杀马特。学长 韩版系。学妹 提交于 2019-12-19 05:20:01
问题 I recently started to use signals in my Django project (v. 1.3) and they all work fine except that I just can't figure out why the m2m_changed signal never gets triggered on my model. The Section instance is edited by adding/deleting PageChild inline instances on an django admin form. I tried to register the callback function either way as described in the documentation, but don't get any result. Excerpt from my models.py from django.db import models from django.db.models.signals import m2m

django many to many duplicate relation issue

守給你的承諾、 提交于 2019-12-13 06:16:41
问题 I need to track invite from users to user. Who users invite who. So I have a User model that have a M2M to "self". class User(AbstractBaseUser, PermissionsMixin): … # referral part invited_by = models.ForeignKey("self", blank=True, null=True, db_index=True) recruits = models.ManyToManyField("self", blank=True, null=True) direct_matches = models.ManyToManyField("self", blank=True, null=True) second_matches = models.ManyToManyField("self", blank=True, null=True) third_matches = models