manytomanyfield

adding products and quantity to an invoice in django

丶灬走出姿态 提交于 2021-01-04 05:39:08
问题 First of all, I am very new to Django and Python and I'm struggling trying to make an VERY simple invoice to work. I have 3 classes; Provider , Product and Invoice . Product and Provider work just fine, I am able to fill them out and they get saved in the database, the thing now is I need to fill an Invoice, selecting a Provider, Products (one or many) and filling the quantity for each product. What I can't seem to find out how to do is: add a quantity to each product selected (I have no clue

ManyToManyField breaking in Admin screens and shouldn't be

时间秒杀一切 提交于 2020-03-25 15:17:22
问题 I have some models like this: class Category(models.Model): class Meta: ordering = ['name'] name = models.CharField(max_length=100) text = models.TextField(blank=True) def __str__(self): return self.name class Tag(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Tool(models.Model): name = models.CharField(max_length=30, null=True, default='') url = models.URLField(max_length=250, null=True, default='') image_url = models.URLField(max_length=250,

How to access generated table fields in Django manytomanyfield

六月ゝ 毕业季﹏ 提交于 2020-02-25 04:32:27
问题 py which looks like below class Scenes(models.Model): name = models.CharField(max_length=30) def __str__(self): return self.name class Tech(models.Model): tech = models.CharField(max_length=30) scenario = models.ManyToManyField('Scenes') def __str__(self): return self.tech class UserLog(models.Model): tech_id = models.ForeignKey(Tech, on_delete=models.CASCADE) ....#other fields It generates one extra table called pages_tech_scenario (pages is my app name) My populated tables in database

How to save multiple ManytoMany instances to a Django object?

元气小坏坏 提交于 2020-01-16 01:16:21
问题 I have a django app with several Question objects. Question and Quiz are related through a ManyToManyField. I want to create Quiz objects such that each quiz is randomly assigned 3 questions. I've tried to create a save method in my model where I: use a queryset to get 3 questions, save the Quiz instance (to save an id), set the queryset to the Quiz and save again. Most of my searches on this topic have shown where people are using a form to save the m2m object. Even better, is it possible to

Model.ManyToManyField.all() gives AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'

北战南征 提交于 2020-01-15 11:49:26
问题 I'm using Django 2.0.2, Python 3.6.4 and PyCharm 2017.3.3 Models: (in models.py) class Position(models.Model): title = models.CharField(max_length=50) gang = models.ForeignKey(Gang, on_delete=models.CASCADE) description = models.TextField(max_length=20000) def __str__(self): return str(self.title) + ', ' + str(self.gang) class Application(models.Model): positions = models.ManyToManyField(Position) applicant = models.ForeignKey(User, on_delete=models.CASCADE) class Ranking(models.Model):

Save m2m in FormView django

无人久伴 提交于 2020-01-07 04:22:07
问题 I'm trying to save a m2m field in a FormView . Here is my code: class ProductorPropietarioView(FormView): form_class = FormPropietario success_url = '/' template_name = 'productores/propietario.html' def form_valid(self,form): form.save(commit=False) form.save() form.save_m2m() return super(ProductorPropietarioView,self).form_valid(form) models.py class Persona(models.Model): predio = models.ForeignKey(InfoPredioGeneral,related_name='predio+') rol = models.ManyToManyField(RolPersona) tipo

How to prefetch_related in reverse of a Django ManyToMany field

懵懂的女人 提交于 2020-01-06 06:44:25
问题 In Django, if I have a ManyToManyField: class Topping(models.Model): name = models.CharField(max_length=30) class Pizza(models.Model): name = models.CharField(max_length=50) toppings = models.ManyToManyField(Topping) I can minimize hits on the database when accessing the Pizza model's toppings field by using prefetch_related(): Pizza.objects.all().prefetch_related('toppings') How can I do the same but in reverse, to prefetch the pizza_set from a Topping queryset? This doesn't work, and I

Django select objects with empty ManyToManyField

痞子三分冷 提交于 2020-01-02 05:58:01
问题 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 回答1: family.kid_set.filter(buyer__isnull=True) should work. 回答2: @piquadrat's answer is correct. You can also do

Django select objects with empty ManyToManyField

人走茶凉 提交于 2020-01-02 05:57:09
问题 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 回答1: family.kid_set.filter(buyer__isnull=True) should work. 回答2: @piquadrat's answer is correct. You can also do