django-models

how to create serializer for an enum field in django rest framework

…衆ロ難τιáo~ 提交于 2020-06-10 10:07:06
问题 i am writing an API in python django rest framework and i am stuck at creating a serializer field for an ENUM, how can i create a serializer field for an ENUM field. my model code is: class Queue(models.Model): class Meta: db_table = 'queues' id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) name = models.CharField(max_length=45) type = EnumChoiceField(QueueTypes, default=QueueTypes.pending) date = models.DateTimeField(auto_now=True) and i am writing a

Django Admin: JSONField default empty dict wont save in admin

时光怂恿深爱的人放手 提交于 2020-06-10 09:19:30
问题 in my model defn i have from django.contrib.postgres.fields import JSONField ..... ....... ......... media_data=JSONField(default=dict) I created a default admin When i attempt to save without touching the field, i get a this field is required error. It looks like a form validation issue because I can programatically save the model instance from code without issue. Why is this happening? have i missed something silly? 回答1: What happening. when dive into the source code. we can see the

Keywords Field in Django Model

落花浮王杯 提交于 2020-06-10 07:21:05
问题 I'm beginner in Django. In my model I have a Field named keywords that I want to store keywords in it (separated by comma). A user may enter a compound keyword "learn Django" or "Django model-Fields" , and also I want to have the possibility to set a max_length for every keyword. And a max ? of keywords! How can I do this please ? 回答1: You can add this feature to any model using django taggit or a similar app. Using an existing app also means you don't have to repeat the work they've already

request.user in Django model

烂漫一生 提交于 2020-06-10 03:45:51
问题 I am using Django models to create the fields for a form. I would like to have the user's username automatically detected in and be filled out, this way I can hide it in my form (instead of having them choose their username from a long list that has everyones username). To do this I am using: current_user = request.user and then setting the default to current_user. However, I keep getting this error: NameError: name 'request' is not defined I'm assuming you can't use requests in Django models

django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD

一笑奈何 提交于 2020-06-09 18:09:43
问题 I wanna add imagefield to my models.py and upload in my media_cdn directory but when i migrate to base my model.py he give this error django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD Output from cmd operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards field, File "C:\Users

How could we multiple foreignkeys in intermediate table in Django.?

情到浓时终转凉″ 提交于 2020-06-09 05:24:06
问题 I have M2M relationship table with an intermediate table along with multiple foreign keys such as Employee Designation and HRManagement . in it each employee has to be reported to one of the existed employees. How could I fix it please help me out in this scenario. class Employee(models.Model): code = models.CharField(max_length = 256) designation =models.ManyToManyField(Designation,through = 'HRManagement',blank=True) status = models.BooleanField(default = True) join_date = models.DateField

django filter queryset show variables on template [duplicate]

允我心安 提交于 2020-06-08 14:20:25
问题 This question already has an answer here : Django-Filter Package: How To Filter Objects To Create Stats And Not Lists (1 answer) Closed 4 days ago . Below is the views.py to my stats page. This page has a bunch of calculations based on my model objects. Works great. However when I apply django-filter to the data it does not change. Example filtering for only "short" trades or in the "last 7 days". I know that get_context_data is basically hardcoding the results and it will not be affected by

Type mismatch in django serializer for request parsing

前提是你 提交于 2020-06-01 07:05:56
问题 I had created the following serializers for request parsing of JSON data. However, while performing the operation, I get an unexpected error. class A(serializers.ModelSerializer): class Meta: model = CName fields = ('id','contact','email') read_only_fields=('contact',) class B(serializers.ModelSerializer): class Meta: model = PName fields = ('id','contact','number') read_only_fields=('contact',) class C(serializers.ModelSerializer): contact_number = A(many=True) contact_email = B(many=True)

Type mismatch in django serializer for request parsing

依然范特西╮ 提交于 2020-06-01 07:03:59
问题 I had created the following serializers for request parsing of JSON data. However, while performing the operation, I get an unexpected error. class A(serializers.ModelSerializer): class Meta: model = CName fields = ('id','contact','email') read_only_fields=('contact',) class B(serializers.ModelSerializer): class Meta: model = PName fields = ('id','contact','number') read_only_fields=('contact',) class C(serializers.ModelSerializer): contact_number = A(many=True) contact_email = B(many=True)

Type mismatch in django serializer for request parsing

白昼怎懂夜的黑 提交于 2020-06-01 07:03:24
问题 I had created the following serializers for request parsing of JSON data. However, while performing the operation, I get an unexpected error. class A(serializers.ModelSerializer): class Meta: model = CName fields = ('id','contact','email') read_only_fields=('contact',) class B(serializers.ModelSerializer): class Meta: model = PName fields = ('id','contact','number') read_only_fields=('contact',) class C(serializers.ModelSerializer): contact_number = A(many=True) contact_email = B(many=True)