django-models

Many models in Foreign Key

我是研究僧i 提交于 2020-08-24 22:36:38
问题 Let's say I have a model, employee : Employee Name: CharField Email: EmailField Type: ForeignKey The problem is with the type field. I want the foreign key to be able to be several different types of models . For example, one for Software Developer , another for Janitor , and so on. I could do this by having a foreign key field for all types, and setting many to null, but this seems bad. Does this make sense? How can I achieve this? Thanks!! 回答1: There are many ways you can model this level

Many models in Foreign Key

℡╲_俬逩灬. 提交于 2020-08-24 22:30:06
问题 Let's say I have a model, employee : Employee Name: CharField Email: EmailField Type: ForeignKey The problem is with the type field. I want the foreign key to be able to be several different types of models . For example, one for Software Developer , another for Janitor , and so on. I could do this by having a foreign key field for all types, and setting many to null, but this seems bad. Does this make sense? How can I achieve this? Thanks!! 回答1: There are many ways you can model this level

Django forms with Foreign keys

≡放荡痞女 提交于 2020-08-24 08:28:08
问题 I have scenario in which a user can have multiple books. I can create two different models for user and books and relate them using foreign keys (or one-to-many will be right way ?). I have created a django forms for User model but when i do like this {{form.as_p}} in templates only user model fields is shown not books field. I want that with user fields my books model filed also displayed (like book names field more then once because he can have multiple books) , Please let me know if it is

Django override bulk_create

巧了我就是萌 提交于 2020-08-24 07:41:07
问题 How to override bulk_create method? I try this class SomeModel(models.Model): field = models.CharField() def bulk_create(self, objs, batch_size=None): #do something return super(SomeModel, self).bulk_create(objs, batch_size) But it doesn't work. When I run this code SomeModel.objects.bulk_create(objects_list) It's create new objects, but it doesn't use my override bulk_create method. Is it possible to override bulk_create? And how? 回答1: bulk_create is a method on the Manager class, and

Django override bulk_create

拜拜、爱过 提交于 2020-08-24 07:41:03
问题 How to override bulk_create method? I try this class SomeModel(models.Model): field = models.CharField() def bulk_create(self, objs, batch_size=None): #do something return super(SomeModel, self).bulk_create(objs, batch_size) But it doesn't work. When I run this code SomeModel.objects.bulk_create(objects_list) It's create new objects, but it doesn't use my override bulk_create method. Is it possible to override bulk_create? And how? 回答1: bulk_create is a method on the Manager class, and

How to set a random integer as the default value for a Django CharField?

*爱你&永不变心* 提交于 2020-08-24 05:57:46
问题 My models.py looks like this: import random random_string = str(random.randint(10000, 99999)) class Content(models.Model): ...... unique_url = models.CharField(default = random_string) When I add a content in admin, an integer in the range is generated and put into the charfield as its default value. From there, I can simple add more words to the charfield. However, the problem with my current set-up is that the integer remains the same every time I add a new article. I want to generate and

How to set a random integer as the default value for a Django CharField?

六月ゝ 毕业季﹏ 提交于 2020-08-24 05:56:30
问题 My models.py looks like this: import random random_string = str(random.randint(10000, 99999)) class Content(models.Model): ...... unique_url = models.CharField(default = random_string) When I add a content in admin, an integer in the range is generated and put into the charfield as its default value. From there, I can simple add more words to the charfield. However, the problem with my current set-up is that the integer remains the same every time I add a new article. I want to generate and