django-models

Dynamical Form fields in __init__in Django admin

☆樱花仙子☆ 提交于 2020-04-13 18:12:12
问题 My model and form: #admin.py class SitesForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(SitesForm, self).__init__(*args, **kwargs) self.fields['mynewfield'] = forms.CharField() class SitesAdmin(admin.ModelAdmin): form = SitesForm admin.site.register(Sites,SitesAdmin) #model.py class Sites(models.Model): url = models.URLField(u'URL') is_active = models.BooleanField(default=True, blank=True) is_new = models.BooleanField(default=False, blank=True) group = models.ForeignKey(

Dynamical Form fields in __init__in Django admin

眉间皱痕 提交于 2020-04-13 18:12:03
问题 My model and form: #admin.py class SitesForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(SitesForm, self).__init__(*args, **kwargs) self.fields['mynewfield'] = forms.CharField() class SitesAdmin(admin.ModelAdmin): form = SitesForm admin.site.register(Sites,SitesAdmin) #model.py class Sites(models.Model): url = models.URLField(u'URL') is_active = models.BooleanField(default=True, blank=True) is_new = models.BooleanField(default=False, blank=True) group = models.ForeignKey(

Django Queryset __in with None value in list

帅比萌擦擦* 提交于 2020-04-12 16:11:32
问题 a = M.objects.filter(f__in=[None, 1]) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IN (None, 1)' dont you think that would be IN (NULL, 1) ? like: a = M.objects.filter(f=None) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IS NULL' Is this a default SQL behavior, django bug or I am missing something with f__in= ? thank you in advance! 回答1: a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 回答2: It seems to be and old bug in Django (https://code

Django Queryset __in with None value in list

不想你离开。 提交于 2020-04-12 16:10:41
问题 a = M.objects.filter(f__in=[None, 1]) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IN (None, 1)' dont you think that would be IN (NULL, 1) ? like: a = M.objects.filter(f=None) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IS NULL' Is this a default SQL behavior, django bug or I am missing something with f__in= ? thank you in advance! 回答1: a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 回答2: It seems to be and old bug in Django (https://code

What is the difference between south migrations and django migrations?

随声附和 提交于 2020-04-12 12:02:10
问题 Can anyone please explain me the difference between south migrations and django migrations? What advantage/disadvantage one has over another? 回答1: South is a third part django app that added support for migrations before a builtin migration solution was introduced in Django 1.7. Unless you're stuck with a long-dead Django version, there's no reason you should use South at all. FWIW, just checking the south project's page should have answered your question: South has been deprecated. From

Graphene Django - Mutation with one to many relation foreign key

℡╲_俬逩灬. 提交于 2020-04-09 18:38:23
问题 I would like to know how to properly create mutation for creating this django model: class Company(models.Model): class Meta: db_table = 'companies' app_label = 'core' default_permissions = () name = models.CharField(unique=True, max_length=50, null=False) email = models.EmailField(unique=True, null=False) phone_number = models.CharField(max_length=13, null=True) address = models.TextField(max_length=100, null=False) crn = models.CharField(max_length=20, null=False) tax = models.CharField(max

ValueError: Related model u'app.model' cannot be resolved

北战南征 提交于 2020-04-07 16:59:28
问题 I have two applications ( ook and eek say) and I want to use a foreign key to a model in ook from a model in eek . Both are in INSTALLED_APPS with ook first. In ook.models.py , i have: class Fubar(models.Model): ... In eek.models.py , I have: class monkey(models.Model): external = models.ForeignKey('ook.Fubar', blank=True, null=True) ... The migration generated is: class Migration(migrations.Migration): dependencies = [ ('eek', '0002_auto_20151029_1040'), ] operations = [ migrations

How to use enums as a choice field in django model

本小妞迷上赌 提交于 2020-04-06 04:25:59
问题 I have a model class of which I want two fields to be a choice fields, so to populate those choices I am using an enum as listed below #models.py class Transaction(models.Model): trasaction_status = models.CharField(max_length=255, choices=TransactionStatus.choices()) transaction_type = models.CharField(max_length=255, choices=TransactionType.choices()) #enums.py class TransactionType(Enum): IN = "IN", OUT = "OUT" @classmethod def choices(cls): print(tuple((i.name, i.value) for i in cls))

How to count the number of rows in a database table in Django

瘦欲@ 提交于 2020-04-06 02:10:38
问题 I have database created by Django model, where status and id and username are the fields of the table Messages . I need to count the number of rows where status value= 0 for a particular username . This is my incomplete code: Message_me = Messages.objects.get(username='myname') 回答1: Try this code: Message_me = Messages.objects.filter(username='myname', status=0).count() 回答2: You can either use Python's len() or use the count() method on any queryset depending on your requirements. Also note,

How to count the number of rows in a database table in Django

笑着哭i 提交于 2020-04-06 02:10:07
问题 I have database created by Django model, where status and id and username are the fields of the table Messages . I need to count the number of rows where status value= 0 for a particular username . This is my incomplete code: Message_me = Messages.objects.get(username='myname') 回答1: Try this code: Message_me = Messages.objects.filter(username='myname', status=0).count() 回答2: You can either use Python's len() or use the count() method on any queryset depending on your requirements. Also note,