django-models

Can a django model have two abstract classes?

梦想的初衷 提交于 2021-02-07 21:53:00
问题 I have this models: class BillHeader(models.Model): billno = models.IntegerField(primary_key=True, blank=True) class BillData(models.Model): price = models.DecimalField(_('Price'), max_digits=12, decimal_places=2) amount = models.DecimalField(_('Amount'), max_digits=6, decimal_places=2) [... rest of the model ...] class Meta: abstract = True class BillFooter(models.Model): total = models.DecimalField(_('Total'), max_digits=12, decimal_places=2) [... rest of the model ...] class Meta: abstract

Can a django model have two abstract classes?

血红的双手。 提交于 2021-02-07 21:51:30
问题 I have this models: class BillHeader(models.Model): billno = models.IntegerField(primary_key=True, blank=True) class BillData(models.Model): price = models.DecimalField(_('Price'), max_digits=12, decimal_places=2) amount = models.DecimalField(_('Amount'), max_digits=6, decimal_places=2) [... rest of the model ...] class Meta: abstract = True class BillFooter(models.Model): total = models.DecimalField(_('Total'), max_digits=12, decimal_places=2) [... rest of the model ...] class Meta: abstract

Django CharField To String

…衆ロ難τιáo~ 提交于 2021-02-07 19:24:29
问题 I'm building a tagging system in Django and would like to allow spaces and other characters in the tag name for display but filter them out and use lower case when matching names etc. To that end I have added a field to my Tag model as so: class Tag(models.Model): name = models.CharField(max_length=200, unique=True) matchname = re.sub("\W+" , "", name.lower()) However I am running into a problem, the CharField is not a string and I cannot for the life of me find out how to convert it to one!

How to delete Django object after countdown?

拜拜、爱过 提交于 2021-02-07 19:19:22
问题 In the platform I am working on we want users to be able to set expiry times on the objects they create. After the countdown they set expires, that object should be deleted. How would you recommend doing this? Edit: I should clarify that the expiry time will vary per object. 回答1: The most common way to do what you are describing is to create a column that stores the time of creation and column for the expiry time and simply consider items deleted after the expiry time. You will have to

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

北战南征 提交于 2021-02-07 19:10:37
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

旧巷老猫 提交于 2021-02-07 19:09:24
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

Django IntegerField returning string(!) - how to coerce to int?

≯℡__Kan透↙ 提交于 2021-02-07 14:41:32
问题 I have an IntegerField declared as below on a model: amount = models.IntegerField() When accessing it, it sometimes returns a string. The proximate cause for this is that it has had a string assigned to it. So far, so unmysterious. It also returns a string even after it has been saved. This strikes me as a little surprising: it would be nice if IntegerField coerced its value to an integer on assignment (or, at the latest, on save), so the user could rely on it being an integer. (My

Django IntegerField returning string(!) - how to coerce to int?

我怕爱的太早我们不能终老 提交于 2021-02-07 14:39:29
问题 I have an IntegerField declared as below on a model: amount = models.IntegerField() When accessing it, it sometimes returns a string. The proximate cause for this is that it has had a string assigned to it. So far, so unmysterious. It also returns a string even after it has been saved. This strikes me as a little surprising: it would be nice if IntegerField coerced its value to an integer on assignment (or, at the latest, on save), so the user could rely on it being an integer. (My

Save Base64 String into Django ImageField

ⅰ亾dé卋堺 提交于 2021-02-07 12:19:02
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

Using existing field values in django update query

♀尐吖头ヾ 提交于 2021-02-07 12:16:34
问题 I want to update a bunch of rows in a table to set the id = self.id. How would I do the below? from metadataorder.tasks.models import Task tasks = Task.objects.filter(task_definition__cascades=False) .update(shared_task_id=self.id) The equivalent SQL would be: update tasks_task t join tasks_taskdefinition d on t.task_definition_id = d.id set t.shared_task_id = t.id where d.cascades = 0 回答1: You can do this using an F expression: from django.db.models import F tasks = Task.objects.filter(task