django-models

Within a Django Model, how can I prevent a delete based on a particular field?

China☆狼群 提交于 2020-02-28 12:29:33
问题 In the following, I have a Post model. A Post object has a status field that can be 'unpublished' or 'published' . if status is 'published' , I'd like to prevent the object from being deleted, and would like to keep this logic encapsulated in the model itself. from model_utils import Choices # from Django-Model-Utils from model_utils.fields import StatusField class Post(model.Models) STATUS = Choices( ('unpublished', _('Unpublished')), ('published', _('Published')), ) ... status = StatusField

Within a Django Model, how can I prevent a delete based on a particular field?

烈酒焚心 提交于 2020-02-28 12:27:49
问题 In the following, I have a Post model. A Post object has a status field that can be 'unpublished' or 'published' . if status is 'published' , I'd like to prevent the object from being deleted, and would like to keep this logic encapsulated in the model itself. from model_utils import Choices # from Django-Model-Utils from model_utils.fields import StatusField class Post(model.Models) STATUS = Choices( ('unpublished', _('Unpublished')), ('published', _('Published')), ) ... status = StatusField

How would I use django.forms to prepopulate a choice field with rows from a model?

亡梦爱人 提交于 2020-02-26 18:26:26
问题 I have a ChoiceField in my form class, presumably a list of users. How do I prepopulate this with a list of users from my User model? What I have now is: class MatchForm(forms.Form): choices = [] user1_auto = forms.CharField() user1 = forms.ChoiceField(choices=choices) user2_auto = forms.CharField() user2 = forms.ChoiceField(choices=choices) def __init__(self): user_choices = User.objects.all() for choice in user_choices: self.choices.append( (choice.id, choice.get_full_name()) ) This doesn't

How would I use django.forms to prepopulate a choice field with rows from a model?

一笑奈何 提交于 2020-02-26 18:25:34
问题 I have a ChoiceField in my form class, presumably a list of users. How do I prepopulate this with a list of users from my User model? What I have now is: class MatchForm(forms.Form): choices = [] user1_auto = forms.CharField() user1 = forms.ChoiceField(choices=choices) user2_auto = forms.CharField() user2 = forms.ChoiceField(choices=choices) def __init__(self): user_choices = User.objects.all() for choice in user_choices: self.choices.append( (choice.id, choice.get_full_name()) ) This doesn't

Difference between 'related_name' and 'related_query_name' attributes in Django?

余生长醉 提交于 2020-02-26 09:04:34
问题 Can you explain the difference between related_name and related_query_name attributes for the Field object in Django ? When I use them, How to use them? Thanks! 回答1: related_name will be the attribute of the related object that allows you to go 'backwards' to the model with the foreign key on it. For example, if ModelA has a field like: model_b = ForeignKeyField(ModelB, related_name='model_as') , this would enable you to access the ModelA instances that are related to your ModelB instance by

Difference between 'related_name' and 'related_query_name' attributes in Django?

陌路散爱 提交于 2020-02-26 09:04:27
问题 Can you explain the difference between related_name and related_query_name attributes for the Field object in Django ? When I use them, How to use them? Thanks! 回答1: related_name will be the attribute of the related object that allows you to go 'backwards' to the model with the foreign key on it. For example, if ModelA has a field like: model_b = ForeignKeyField(ModelB, related_name='model_as') , this would enable you to access the ModelA instances that are related to your ModelB instance by

Django Rest Api - ManyToManyField, Display 'title' instead of 'id' in the Exercises Array

本秂侑毒 提交于 2020-02-26 03:47:45
问题 Django Rest Api - ManyToManyField, Display 'title' instead of 'id' in the Exercises Array HTTP 200 OK Allow: GET, POST, PUT, DELETE, PATCH Content-Type: application/json Vary: Accept [ { "id": 1, "title": "Push Workout Bjarred", "description": "Kör Hårt!", "exercises": [ 3, 4, 5, 6, 9, 10 ], "cardio": [ 4 ] }, { "id": 2, "title": "Pull Workout Loddekopinge", "description": "", "exercises": [ 1, 2, 7, 8 ], "cardio": [] }, { "id": 3, "title": "CardioPass", "description": "", "exercises": [],

Django signals post_save update

非 Y 不嫁゛ 提交于 2020-02-26 00:53:12
问题 When the student enrollment record (discount type) is updated the student discount (discount type) is also updated, but what should i do if i want to update the student enrollment record (discount type) using student discount (discount type) This is my models.py class studentDiscount(models.Model): Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+', on_delete=models.CASCADE, null=True) Discount_Type = models.ForeignKey(Discount, related_name='+', on

Django: Help overwriting a model's save() method to auto populate a CharField based on a function on object creation

你。 提交于 2020-02-25 07:03:09
问题 I have a model (Meal) with several many to many fields (proteins, carbohydrates and fats), which I recently added a 'name' CharField to. I wanted to allow the user to enter a name for a meal, but if they do not enter a name, I want the name to be populated automatically based on the function definitions I have in the model which just concatenate the names of the foods in one string. I was trying to follow this guide. Now, if the Meal already exists, what I have actually works fine. However,

Django: Help overwriting a model's save() method to auto populate a CharField based on a function on object creation

三世轮回 提交于 2020-02-25 07:02:09
问题 I have a model (Meal) with several many to many fields (proteins, carbohydrates and fats), which I recently added a 'name' CharField to. I wanted to allow the user to enter a name for a meal, but if they do not enter a name, I want the name to be populated automatically based on the function definitions I have in the model which just concatenate the names of the foods in one string. I was trying to follow this guide. Now, if the Meal already exists, what I have actually works fine. However,