manytomanyfield

Django: AJAX ManyToManyField in admin

自作多情 提交于 2019-12-09 06:25:15
问题 I want to display ManyToManyField s in admin just like filter_horizontal does, but populate the options as the user types into the filter field. There are many options and loading them all at once takes a lot of time. I found django-ajax-filtered-fields but it seems to me an overkill as it requires changes to model classes, when all I want to do is to replace every multiple select field in a form. Writing a custom widget field that inherits from admin.widgets.FilteredSelectMultiple seems to

How do I tell Django to not create a table for an M2M related field?

纵然是瞬间 提交于 2019-12-08 08:14:50
问题 I'm using this little jewel of a Django code snippet to edit a ManyToManyField from both directions: class ManyToManyField_NoSyncdb(models.ManyToManyField): def __init__(self, *args, **kwargs): super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs) self.creates_table = False class Job(models.Model): persons = ManyToManyField_NoSyncdb( Person, blank=True, db_table='person_jobs' ) (snippet details here) It lets me select all the persons in a given job from the jobs form and inversely

ManyToManyField widget in a django admin change list?

浪子不回头ぞ 提交于 2019-12-08 03:40:30
问题 In the change list of the django admin, I want to use list_editable to display a django-autocomplete widget for a ManyToManyField. I found something similar here: list_editable and widgets Normally including a ManyToManyField in list_display raises an ImproperlyConfigured exception, eg: ""'BookAdmin.list_display[2]', 'author' is a ManyToManyField which is not supported." I (perhaps unwisely) removed 3 lines from contrib/admin/validate.py to bypass the exception. :) I now have it spitting out

django manytomany through

做~自己de王妃 提交于 2019-12-07 16:38:39
问题 If I have two Models that have a manytomany relationship with a through model, how do I get data from that 'through' table. class Bike(models.Model): nickname = models.CharField(max_length=40) users = models.ManyToManyField(User, through='bike.BikeUser') The BikeUser class class BikeUser(models.Model): bike = models.ForeignKey(Bike) user = models.ForeignKey(User) comment = models.CharField(max_length=140) And I would add a user to that bike (presuming I have a myBike and a myUser already)

ManyToManyField widget in a django admin change list?

北慕城南 提交于 2019-12-06 13:26:35
In the change list of the django admin, I want to use list_editable to display a django-autocomplete widget for a ManyToManyField. I found something similar here: list_editable and widgets Normally including a ManyToManyField in list_display raises an ImproperlyConfigured exception, eg: ""'BookAdmin.list_display[2]', 'author' is a ManyToManyField which is not supported." I (perhaps unwisely) removed 3 lines from contrib/admin/validate.py to bypass the exception. :) I now have it spitting out the following, which is close(?) but no cigar. <django.db.models.fields.related.ManyRelatedManager

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

安稳与你 提交于 2019-12-06 03:17:38
问题 I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self):

django manytomany through

╄→尐↘猪︶ㄣ 提交于 2019-12-06 02:10:47
If I have two Models that have a manytomany relationship with a through model, how do I get data from that 'through' table. class Bike(models.Model): nickname = models.CharField(max_length=40) users = models.ManyToManyField(User, through='bike.BikeUser') The BikeUser class class BikeUser(models.Model): bike = models.ForeignKey(Bike) user = models.ForeignKey(User) comment = models.CharField(max_length=140) And I would add a user to that bike (presuming I have a myBike and a myUser already) BikeUser.objects.create(bike = myBike, user = myUser, comment = 'Got this one at a fancy store') I can get

Django select objects with empty ManyToManyField

ぐ巨炮叔叔 提交于 2019-12-05 13:00:24
Considering the following models, knowing a family, how do I select Kids with no buyers? class Family... class Kid(models.Model): name = models.CharField(max_length=255) family = models.ForeignKey(Family) buyer = models.ManyToManyField(Buyer, blank=True, null=True) family = get_object_or_404(Family, pk=1) for_sale = family.kid_set.filter(buyer... this screws my child trade business family.kid_set.filter(buyer__isnull=True) should work. Manoj Govindan @piquadrat's answer is correct. You can also do: for_sale = Kid.objects.filter(family__pk = 1, buyer = None) This lets you avoid a separate query

django data from manytomanyfield intermediate table

安稳与你 提交于 2019-12-05 06:42:36
问题 Currently, I have the following model structure set up in my models.py (stripped down): class Admin(models.Model): admin_name = models.CharField(unique = True, blank = False, null = False, max_length = 128, verbose_name = u'admin full name') def __unicode__(self): return self.admin_name class Meta: ordering = ('id',) verbose_name = u'Admin Info' class Project(models.Model): client = models.ForeignKey(Client, verbose_name = u'Client') description = models.ForeignKey(Description, verbose_name =

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

喜夏-厌秋 提交于 2019-12-04 08:53:14
I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self): return self.name I'm having all kinds of fun trying to get the limit_choices_to working on the Gallery model