manytomanyfield

django ManyToMany show related objects

南楼画角 提交于 2019-12-24 22:03:16
问题 Searched everywhere but couldn't find anything even though it feels so simple. So basically i have two classes in my models.py class Restaurant(models.Model): restaurant_title = models.CharField(max_length=30) location = CountryField(null=True, blank_label='(select country)') first_purchase_discount = models.BooleanField(default=False) updated = models.DateTimeField(auto_now=True) slug = models.SlugField(max_length=30, unique=True) def save (self, *args, **kwargs): self.slug = slugify(self

Django: Saving multiple ManyToMany fields within a transaction

折月煮酒 提交于 2019-12-23 09:38:16
问题 this is the representation of my models: class B(models.Model): """I'm a dummy model, so doesn't pay atention of what I do""" name = models.CharField(max_length=250) class A(models.Model): name = models.CharField(max_length=250) many_b = models.ManyToManyField(B) Now, suppose I have a list of B objects. And a single A object that will be related to that B s. Something like this: a = A.objects.get(id=1) list_of_b = [B<name='B1'>,B<name='B2'>,B<name='B3'>,] The way I relate them now is this:

Django: Cannot resolve keyword '' into field. Choices are:

北城余情 提交于 2019-12-21 16:19:47
问题 I am having this weird problem accessing ManyToManyField . I have following models. class Link(models.Model): title = models.CharField(max_length = 200) url = models.URLField(unique = True) tags = models.ManyToManyField(Tag) creation_date = models.DateTimeField(auto_now_add = True) user = models.ForeignKey(User) likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_likes") dis_likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_dis

Show a ManyToManyField as Checkboxes in Django Admin

假装没事ソ 提交于 2019-12-20 10:25:24
问题 Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin?? Any suggestions? 回答1: From this answer it seems like it is possible to use ModelAdmin.formfield_overrides to override the ManyToManyField to use CheckBoxSelectMultiple: from django.db import models from django.contrib import admin from django.forms import CheckboxSelectMultiple class MyModelAdmin(admin.ModelAdmin): formfield_overrides = { models.ManyToManyField: {'widget': CheckboxSelectMultiple}, } I haven't

How can I store history of ManyToManyField using django-simple-history.

自古美人都是妖i 提交于 2019-12-20 04:48:40
问题 How can I store history of ManyToManyField using django-simple-history. I used HistoricalRecords with attribute m2m_filds but it is throwing error: unexpected keyword argument 'm2m_fields' 回答1: I'm macro1 on GitHub, and I guess de facto maintainer of django-simple-history. From your question it seems that you're just asking about general ManyToManyField support compared with other fields. The short answer is that we do not currently support it. ManyToManyFields actually create an in-between

django manytomanyfield .add() method

回眸只為那壹抹淺笑 提交于 2019-12-19 02:52:08
问题 Suppose i have : class Album(models.Model): photos = models.ManyToManyField('myapp.Photo') photo_count = models.IntegerField(default = 0) class Photo(models.Model): photo = models.ImageField(upload_to = 'blahblah') What is want is, everytime i call .add() method, increment the photo_count on Album class, so i'm thinking to override .add() method. The problem is, i can't import the .add() 's class since it is inside a method, so it is like def->class->def . So is there anyway to override .add(

django manytomanyfield .add() method

落花浮王杯 提交于 2019-12-19 02:52:07
问题 Suppose i have : class Album(models.Model): photos = models.ManyToManyField('myapp.Photo') photo_count = models.IntegerField(default = 0) class Photo(models.Model): photo = models.ImageField(upload_to = 'blahblah') What is want is, everytime i call .add() method, increment the photo_count on Album class, so i'm thinking to override .add() method. The problem is, i can't import the .add() 's class since it is inside a method, so it is like def->class->def . So is there anyway to override .add(

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

橙三吉。 提交于 2019-12-18 11:39:12
问题 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 this: source.items.filter(timestamp__lte=some_datetime) How do I efficiently remove all of the items that match that query? I suppose I could try something like this: items_to_remove = list(source.items.filter(timestamp__lte=some_datetime)) source.items.remove(

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

旧街凉风 提交于 2019-12-18 10:00:27
问题 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 ValuesListQueryset seems to fail also. Is there a better way than use a for loop ? 回答1: Use: object.m2mfield.add(*items) as described in the documentation: add() accepts an arbitrary number of arguments, not a list of them. add(obj1, obj2, obj3, ...) To

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

强颜欢笑 提交于 2019-12-18 10:00:04
问题 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 ValuesListQueryset seems to fail also. Is there a better way than use a for loop ? 回答1: Use: object.m2mfield.add(*items) as described in the documentation: add() accepts an arbitrary number of arguments, not a list of them. add(obj1, obj2, obj3, ...) To