django-models

Django one to many get all underlying records

一世执手 提交于 2021-01-29 09:18:51
问题 I'am creating a django application with a database including the following tables: I created the models as following: class Group(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) class Meta: db_table = 'Group' class Filters(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) group_id = models.ForeignKey(Group, on_delete=models.CASCADE,

Adding an item to many to many after creation in django

情到浓时终转凉″ 提交于 2021-01-29 09:15:19
问题 Recently I've been trying to do something with this. Think of the family as a facebook group. class Family(models.Model): name = models.CharField(max_length=50) owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='owned_families') users = models.ManyToManyField(User, related_name='families', blank=True) let's assume we have this family object called fm , for illustration purpose. My problem is, The owner is one of the users right? I mean, When someone creates a family, He's

How to access foreign key in a django template? (DetailView)

橙三吉。 提交于 2021-01-29 08:56:54
问题 I want to display products in a Campaign DetailView template. In my project each campaign has a shop which contains products, so the flow is like, Campaign --> Shop --> Products Campaign models.py class Campaign(models.Model): team = models.OneToOneField(Team, related_name='campaigns', on_delete=models.CASCADE, null=True, blank=True) shop = models.OneToOneField(shop_models.Shop, on_delete=models.CASCADE, null=True, blank=True) Shop models.py class Product(models.Model): title = models

Django annotate returning unexpected Sum value

﹥>﹥吖頭↗ 提交于 2021-01-29 08:55:53
问题 These are my models: class Consume(models.Model): amount = models.FloatField(default=1) entry_for = models.ForeignKey( Person, on_delete=models.SET_NULL, related_name='consume_entry_for', ) class Purchase(models.Model): amount = models.DecimalField( max_digits=6, decimal_places=2, default=0.00 ) entry_for = models.ForeignKey( Person, on_delete=models.CASCADE, related_name='ledger_entry_for', ) and this is my query: person_wise_total = Person.objects.annotate( total_purchase=Coalesce(Sum(

How can I divide the request.POST to fit 2 distinct forms in django?

时光毁灭记忆、已成空白 提交于 2021-01-29 08:46:14
问题 I would like to know whether it is possible to divide the request.POST attribute in django in order to fill 2 or more distinct forms. Because my Main model operates in a form that it is related to 2 child models with a ManyToManyField(), I want to create more instances of this model using forms. A quick Example: class ChildModelOne(models.Model): title_one = models.CharField(max_length = 64) list = models.ManyToManyField(otherModel, blank = True, default = None) def __str__(self): return self

Django Admin Issue - 'NoneType' object has no attribute 'user'

天涯浪子 提交于 2021-01-29 08:25:13
问题 Writing a blog engine with Django 1.5 and using the Django Admin. Everything was fine, until I added a ManyToManyField to a model, and added that field to the ModelAdmin's fieldsets, then I started to get this mysterious error when trying to load the admin page: 'NoneType' object has no attribute 'user' (Full stack trace further on below.) Why would the response object suddenly be None? If I remove the field from the fieldset, everything's fine again. My models look something a bit like this

Validate Multiple files in django

笑着哭i 提交于 2021-01-29 08:13:57
问题 I want to allow some certain file types to be uploaded. I wrote the below code for one certain file, it worked. def validate_file_extension(value): if not value.name.endswith('.zip'): raise ValidationError(u'Error message') but I want to allow more than one files, so I set those files in settings_dev, and wrote the below code, but not working. def validate_file_extension(value): for f in settings_dev.TASK_UPLOAD_FILE_TYPES: if not value.name.endswith(f): raise ValidationError(u'Error message'

Django infer a field from other fields

北战南征 提交于 2021-01-29 08:00:38
问题 I am currently having a model: class Current(models.Model): field1 = models.IntegerField() field2 = models.IntegerField() field3 = models.IntegerField() I need to have field3 to be set directly equal to field1 + field2 without actually sending it. What's the standard way in django to do this? PS: Yes, I need to save field3 in the database alongwith the other fields. 回答1: Something like this? But not sure why this would need to be saved in the database. class Current(models.Model): field1 =

module 'qrcode' has no attribute 'make'

≯℡__Kan透↙ 提交于 2021-01-29 07:27:10
问题 While integrating python library qrcode==6.1 with django==3.1.2. I have been trying to generate a qrcode which will contain the URL links for my other websites. Models.py from django.db import models import qrcode from io import BytesIO from django.core.files import File from PIL import Image, ImageDraw # Create your models here. class Website(models.Model): name = models.CharField(max_length=200) qr_code = models.ImageField(upload_to='qr_codes', blank=True) def __str__(self): return str(self

How to register inherited sub class in admin.py file in django?

北战南征 提交于 2021-01-29 07:21:06
问题 Project Name : fusion App Name : admin_lte Python 3.7 Django 2 MySql Question is "I want to register sub model in django admin-panel",when i write code for model registration in admin.py file that time occurred below error. django.core.exceptions.ImproperlyConfigured: The model Device is abstract, so it cannot be registered with admin. NOTE : I used multiple separated model file. device.py (Model File) from django.db import models class Device(models.Model): device_type = models.CharField(max