django-models

Django/Python update field values (during model save)

◇◆丶佛笑我妖孽 提交于 2021-02-07 03:32:59
问题 I am trying to capitalize a number of fields in my django models, when they are saved. Looking at this question, which had this answer: class Blog(models.Model): name = models.CharField(max_length=100) def save(self): self.name = self.name.title() super(Blog, self).save() This works fine, however, in each save, this requires some extra typing, if I want to repeat this multiple times. So I would like to create a function that takes fields as an input and re-saves them as uppercase, during the

How to test django model method __str__()

一世执手 提交于 2021-02-06 16:06:04
问题 I trying to test __str__ method, and when trying to access it in my test it returns my model instance (I think it is) def test_str_is_equal_to_title(self): """ Method `__str__` should be equal to field `title` """ work = Work.objects.get(pk=1) self.assertEqual(work.__str__, work.title) And from test I get: AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title' How I should compare these 2 values to pass test? 回答1: According to the documentation: Model.__str__()

How to test django model method __str__()

醉酒当歌 提交于 2021-02-06 16:05:02
问题 I trying to test __str__ method, and when trying to access it in my test it returns my model instance (I think it is) def test_str_is_equal_to_title(self): """ Method `__str__` should be equal to field `title` """ work = Work.objects.get(pk=1) self.assertEqual(work.__str__, work.title) And from test I get: AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title' How I should compare these 2 values to pass test? 回答1: According to the documentation: Model.__str__()

How to test django model method __str__()

假装没事ソ 提交于 2021-02-06 16:04:31
问题 I trying to test __str__ method, and when trying to access it in my test it returns my model instance (I think it is) def test_str_is_equal_to_title(self): """ Method `__str__` should be equal to field `title` """ work = Work.objects.get(pk=1) self.assertEqual(work.__str__, work.title) And from test I get: AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title' How I should compare these 2 values to pass test? 回答1: According to the documentation: Model.__str__()

Custom user model in django

被刻印的时光 ゝ 提交于 2021-02-06 15:26:24
问题 I want to create a custom user model using django.contrib.auth.models.AbstractUser as stated in the djangodocs: If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model. So I inherited the AbstractUser class in my Users class and added a field. But when I

Custom user model in django

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-06 15:24:58
问题 I want to create a custom user model using django.contrib.auth.models.AbstractUser as stated in the djangodocs: If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model. So I inherited the AbstractUser class in my Users class and added a field. But when I

Custom user model in django

早过忘川 提交于 2021-02-06 15:24:28
问题 I want to create a custom user model using django.contrib.auth.models.AbstractUser as stated in the djangodocs: If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model. So I inherited the AbstractUser class in my Users class and added a field. But when I

Custom user model in django

China☆狼群 提交于 2021-02-06 15:23:02
问题 I want to create a custom user model using django.contrib.auth.models.AbstractUser as stated in the djangodocs: If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model. So I inherited the AbstractUser class in my Users class and added a field. But when I

How to create a unique_for_field slug in Django?

你离开我真会死。 提交于 2021-02-06 12:00:30
问题 Django has a unique_for_date property you can set when adding a SlugField to your model. This causes the slug to be unique only for the Date of the field you specify: class Example(models.Model): title = models.CharField() slug = models.SlugField(unique_for_date='publish') publish = models.DateTimeField() What would be the best way to achieve the same kind of functionality for a non-DateTime field like a ForeignKey? Ideally, I want to do something like this: class Example(models.Model): title

How to create a unique_for_field slug in Django?

家住魔仙堡 提交于 2021-02-06 11:56:38
问题 Django has a unique_for_date property you can set when adding a SlugField to your model. This causes the slug to be unique only for the Date of the field you specify: class Example(models.Model): title = models.CharField() slug = models.SlugField(unique_for_date='publish') publish = models.DateTimeField() What would be the best way to achieve the same kind of functionality for a non-DateTime field like a ForeignKey? Ideally, I want to do something like this: class Example(models.Model): title