m2m

Django - How to save m2m data via post_save signal?

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: (Django 1.1) I have a Project model that keeps track of its members using a m2m field. It looks like this: class Project ( models . Model ): members = models . ManyToManyField ( User ) sales_rep = models . ForeignKey ( User ) sales_mgr = models . ForeignKey ( User ) project_mgr = models . ForeignKey ( User ) ... ( more FK user fields ) ... When the project is created, the selected sales_rep , sales_mgr , project_mgr , etc User s are added to members to make it easier to keep track of project permissions. This approach has worked

Display m2m field defined via 'through' in admin

旧城冷巷雨未停 提交于 2019-12-03 07:13:21
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 category, and book_category relationship. If I include categories in BookAdmin as class BookAdmin(admin

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

*爱你&永不变心* 提交于 2019-12-03 06:13:20
An example Many-to-Many through relationship in Django: class First(models.Model): seconds = models.ManyToManyField(Second, through='Middle') class Middle(models.Model): first = models.ForeignKey(First) second = models.ForeignKey(Second) class Second(models.Model): Following the documentation on intermediary models , only one model of the pair to be related contains the ManytoManyField , model First in the example above. Is this correct? If so, which model should contain the ManytoManyField field? Are there any differences in using the relationship from either end depending on where the

Django accessing ManyToMany fields from post_save signal

匿名 (未验证) 提交于 2019-12-03 02:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a Django model and I want to modify the object permissions on or just after save. I have tried a few solutions and the post_save signal seemed the best candidate for what I want to do: class Project ( models . Model ): title = models . CharField ( max_length = 755 , default = 'default' ) assigned_to = models . ManyToManyField ( User , default = None , blank = True , null = True ) created_by = models . ForeignKey ( User , related_name = "%(app_label)s_%(class)s_related" ) @receiver ( post_save , sender = Project ) def assign

Avoid Django def post duplicating on save

痞子三分冷 提交于 2019-12-02 15:03:23
问题 Hi I'm facing issues of duplicated objects when saving. How can I prevent that? Thanks in advance. #models.py class Candidate(models.Model): user = models.OneToOneField(User, primary_key=True) birth = models.CharField(max_length=50) ... class Job(models.Model): candidate = models.ManyToManyField('Candidate', through='CandidateToJob') title = models.CharField(max_length=500) ... class CandidateToJob(models.Model): job = models.ForeignKey(Job, related_name='applied_to') candidate = models

django restframework - Serializer for creating m2m relationship

会有一股神秘感。 提交于 2019-12-02 12:32:51
问题 I am using django-restframework for my API. I created a serializer which should list my objects and should be able to create them. I have a Contact entity and Product . There is a standard m2m between it for the likes . Here's my model: class Contact(models.Model): ... products_of_interest = models.ManyToManyField(Product, related_name="interested_contacts") My serializer is as simple as it gets: class ContactSerializer(serializers.ModelSerializer): class Meta: model = Contact When I list my

django restframework - Serializer for creating m2m relationship

删除回忆录丶 提交于 2019-12-02 04:43:06
I am using django-restframework for my API. I created a serializer which should list my objects and should be able to create them. I have a Contact entity and Product . There is a standard m2m between it for the likes . Here's my model: class Contact(models.Model): ... products_of_interest = models.ManyToManyField(Product, related_name="interested_contacts") My serializer is as simple as it gets: class ContactSerializer(serializers.ModelSerializer): class Meta: model = Contact When I list my contacts via this serializer I get all my contact objects with an array of product ids - awesome:

Django signal m2m_changed not triggered

牧云@^-^@ 提交于 2019-12-01 02:57:46
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_changed class Section(models.Model): name = models.CharField(unique = True, max_length = 100) pages =

Django Tastypie not Updating Resource with ManyToManyField

给你一囗甜甜゛ 提交于 2019-11-29 08:48:50
Why doesn't my resource with a ManyToManyField update with this PUT request? curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"uuid":"blah","pass_token":"blah","favorites": ["/api/v1/organizations/1/"]}' http://localhost:8000/api/v1/devices/2/ I get this response: HTTP/1.0 400 BAD REQUEST Date: Wed, 11 Jul 2012 22:21:15 GMT Server: WSGIServer/0.1 Python/2.7.2 Content-Type: application/json; charset=utf-8 {"favorites": ["\"/api/v1/organizations/1/\" is not a valid value for a primary key."]} Here are my resources: class OrganizationResource(ModelResource): parent_org =

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

廉价感情. 提交于 2019-11-28 08:28:35
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 assining m2m filed in view(views.py) from form(forms.py). How to assign m2m field in view. Django version 2.O python 3.5 models.py class User(AbstractUser): username=models.CharField(max_length=20) email = models.EmailField(_('email address'), unique=True) class Setupuser(models.Model): organization=models.CharField(max_length=200,blank=False,null=True) emails_for_help = models.ManyToManyField(User) views.py class Set_user