django-models

Django model field name “check” raises SystemCheckError

最后都变了- 提交于 2021-02-10 12:31:02
问题 The Django docs state there are only two restrictions on model field names A field name cannot be a Python reserved word A field name cannot contain more than one underscore in a row However, given the following example, it doesn't look like I can use the field name check as a ForeignKey. class Check(models.Model): name = models.CharField(max_length=100) class MyModel(models.Model): # this works fine #check = models.BooleanField() # this breaks check = models.ForeignKey(Check, on_delete

django models that contains list of another model

百般思念 提交于 2021-02-10 11:50:31
问题 I am trying to make a django model that can have a list of another model as an attribute def author: name = charfield... list_of_books = ______________ def book: name = charfield... I want an author to be able to have a list of books he has written. And I would like to know how I can add a book to the authors list of books. I have seen foreignKey, but It seems like that it only to hold one object. 回答1: Use a ForeignKey in the other direction. book should contain a ForeignKey to author, and,

django models that contains list of another model

末鹿安然 提交于 2021-02-10 11:47:29
问题 I am trying to make a django model that can have a list of another model as an attribute def author: name = charfield... list_of_books = ______________ def book: name = charfield... I want an author to be able to have a list of books he has written. And I would like to know how I can add a book to the authors list of books. I have seen foreignKey, but It seems like that it only to hold one object. 回答1: Use a ForeignKey in the other direction. book should contain a ForeignKey to author, and,

Custom field in Django get_prep_value() has no effect

和自甴很熟 提交于 2021-02-10 11:46:37
问题 So, I've made a similar class to this answer. It looks like: class TruncatingCharField(models.CharField): description = _("String (truncated to %(max_length)s)") def get_prep_value(self, value): value = super(TruncatingCharField, self).get_prep_value(value) if value: value = value[:self.max_length] return value I would expect that instantiating a model with this field with strings longer than the threshold should trigger truncation. However this appears not to be the case: class

Custom field in Django get_prep_value() has no effect

馋奶兔 提交于 2021-02-10 11:46:36
问题 So, I've made a similar class to this answer. It looks like: class TruncatingCharField(models.CharField): description = _("String (truncated to %(max_length)s)") def get_prep_value(self, value): value = super(TruncatingCharField, self).get_prep_value(value) if value: value = value[:self.max_length] return value I would expect that instantiating a model with this field with strings longer than the threshold should trigger truncation. However this appears not to be the case: class

Django Get ID of the object in save()

雨燕双飞 提交于 2021-02-10 07:10:36
问题 I have some fixed number let say it is num = 1000 Now I have field which need to be sum of the object.id and num. I need this in save() something like below. def save(self, *args, **kwargs): num = 1000 self.special = self.id + num super(MyModel, self).save(*args, **kwargs) But self.id is known after save and field named special is required. How to get self.id? 回答1: First of all? Is the number you're trying to add a fixed number? If so, why do you have to store it in the db at all? You may

Django Get ID of the object in save()

北城余情 提交于 2021-02-10 07:08:02
问题 I have some fixed number let say it is num = 1000 Now I have field which need to be sum of the object.id and num. I need this in save() something like below. def save(self, *args, **kwargs): num = 1000 self.special = self.id + num super(MyModel, self).save(*args, **kwargs) But self.id is known after save and field named special is required. How to get self.id? 回答1: First of all? Is the number you're trying to add a fixed number? If so, why do you have to store it in the db at all? You may

Django Get ID of the object in save()

亡梦爱人 提交于 2021-02-10 07:07:09
问题 I have some fixed number let say it is num = 1000 Now I have field which need to be sum of the object.id and num. I need this in save() something like below. def save(self, *args, **kwargs): num = 1000 self.special = self.id + num super(MyModel, self).save(*args, **kwargs) But self.id is known after save and field named special is required. How to get self.id? 回答1: First of all? Is the number you're trying to add a fixed number? If so, why do you have to store it in the db at all? You may

ForeignKey to AnonymousUser

♀尐吖头ヾ 提交于 2021-02-10 06:48:13
问题 I would like to follow this guideline and avoid a nullable ForeignKey. I have a ForeignKey to the django User model. If I try to store request.user in an instance of this model, I get this error: ValueError: Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser>>": "MyModel.user" must be a "User" instance. I think it is feasible to avoid the non-conditionless data schema (nullable foreign key). How could I solve this? 回答1: Those guidelines suggest you create an instance

django - Unsupported lookup for JSONField or join on the field not permitted

爱⌒轻易说出口 提交于 2021-02-09 11:10:16
问题 I am having a Json field in my models as - class Product(models.Model): ... detailed_stock = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict},default=dict) I am having values in my database like - { "total":0, "5[1]":0 } I am trying to filter objects with total = 0, for that I tried - Product.objects.filter(detailed_stock__total = 0) but it throws error - Unsupported lookup 'total' for JSONField or join on the field not permitted. as per the documentation the following