django-models

InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]

為{幸葍}努か 提交于 2020-01-22 07:17:24
问题 When I run tests I get this error during database initialization: django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>] This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth) I created this proxy for contrib.auth Group model to place it in my app in django admin: class GroupProxy(Group): class Meta: proxy = True verbose_name = Group._meta.verbose_name verbose_name_plural = Group._meta.verbose_name_plural

InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]

杀马特。学长 韩版系。学妹 提交于 2020-01-22 07:16:07
问题 When I run tests I get this error during database initialization: django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>] This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth) I created this proxy for contrib.auth Group model to place it in my app in django admin: class GroupProxy(Group): class Meta: proxy = True verbose_name = Group._meta.verbose_name verbose_name_plural = Group._meta.verbose_name_plural

django form doesn't work on click submit

∥☆過路亽.° 提交于 2020-01-22 03:19:51
问题 I'm trying to do a form, with gender choices. The user could choice between male or female. What I have now in forms.py: class GenderForm(forms.Form): demo = DemoData.objects.all() GENDER_CHOICES = [ ('Male', 'Masculino'), ('Female', 'Feminino')] gender = forms.ModelChoiceField(demo, widget=Select(), required=True) choices_distlabel = [('', '')] + GENDER_CHOICES gender.choices = choices_distlabel in the template: <form action="" method="post"> {% for field in form_gender %} {{ field }} {%

Django - counting model instance views (for a “top entries” app)

核能气质少年 提交于 2020-01-21 10:26:20
问题 I'm new, and confused. I want to create a module that keeps track of the "top hit" instances of both an article and a blog model. I don't want to touch the code for the article or blog models. Is this a candidate for middleware? looking at the HttpRequest.path ? 回答1: Middleware looking at request.path is ugly, as it introduces a dependency on the details of the URL patterns you use to display articles and blog posts. If you don't mind this coupling, then you might as well just save the

Django - counting model instance views (for a “top entries” app)

不羁岁月 提交于 2020-01-21 10:26:09
问题 I'm new, and confused. I want to create a module that keeps track of the "top hit" instances of both an article and a blog model. I don't want to touch the code for the article or blog models. Is this a candidate for middleware? looking at the HttpRequest.path ? 回答1: Middleware looking at request.path is ugly, as it introduces a dependency on the details of the URL patterns you use to display articles and blog posts. If you don't mind this coupling, then you might as well just save the

adding the same object twice to a ManyToManyField

拈花ヽ惹草 提交于 2020-01-21 04:31:12
问题 I have two django model classes: class A(models.Model): name = models.CharField(max_length = 128) #irrelevant class B(models.Model): a = models.ManyToManyField(A) name = models.CharField(max_length = 128) #irrelevant What I want to do is the following: a1 = A() a2 = A() b = B() b.a.add(a1) b.a.add(a1) #I want to have a1 twice b.a.add(a2) assert len(b.a.all()) == 3 #this fails; the length of all() is 2 I am guessing that add() uses a set semantic, but how can I circumvent that? I tried looking

How do I reference a Django settings variable in my models.py?

喜夏-厌秋 提交于 2020-01-20 12:29:29
问题 This is a very beginner question. But I'm stumped. How do I reference a Django settings variable in my model.py? NameError: name 'PRIVATE_DIR' is not defined Also tried a lot of other stuff including settings.PRIVATE_DIR settings.py: PRIVATE_DIR = '/home/me/django_projects/myproject/storage_dir' models.py: # Problem is here. from django.core.files.storage import FileSystemStorage fs = FileSystemStorage(location=PRIVATE_DIR) class Customer(models.Model): lastName = models.CharField(max_length

How do I reference a Django settings variable in my models.py?

不想你离开。 提交于 2020-01-20 12:28:41
问题 This is a very beginner question. But I'm stumped. How do I reference a Django settings variable in my model.py? NameError: name 'PRIVATE_DIR' is not defined Also tried a lot of other stuff including settings.PRIVATE_DIR settings.py: PRIVATE_DIR = '/home/me/django_projects/myproject/storage_dir' models.py: # Problem is here. from django.core.files.storage import FileSystemStorage fs = FileSystemStorage(location=PRIVATE_DIR) class Customer(models.Model): lastName = models.CharField(max_length

How do I reference a Django settings variable in my models.py?

こ雲淡風輕ζ 提交于 2020-01-20 12:28:26
问题 This is a very beginner question. But I'm stumped. How do I reference a Django settings variable in my model.py? NameError: name 'PRIVATE_DIR' is not defined Also tried a lot of other stuff including settings.PRIVATE_DIR settings.py: PRIVATE_DIR = '/home/me/django_projects/myproject/storage_dir' models.py: # Problem is here. from django.core.files.storage import FileSystemStorage fs = FileSystemStorage(location=PRIVATE_DIR) class Customer(models.Model): lastName = models.CharField(max_length

Am I overriding the save method on the model in a wrong way in django?

不羁岁月 提交于 2020-01-20 08:48:11
问题 I have a Profile model that extends the user model like so, class Profile(User): user = models.OneToOneField(User, parent_link=True, on_delete=models.CASCADE) slug = models.SlugField(unique=True, blank=True) def save(self, *args, **kwargs): print('self.username') print(self.username) self.slug = self.username super(Profile, self).save(*args, **kwargs) I am trying to create a slug field for my model , so I override the save method to include the slug as the username. The thing is, when I