django-models

ArrayField with JSONField as base_field in Django

喜你入骨 提交于 2021-01-28 09:01:16
问题 I have a GooglePlace model with a field to store the address_components being returned by Google Places API. model.py address_components = ArrayField(JSONField(), null=True, blank=True) I am trying to store the data like this address_components = [component for component in google_place_details.get("address_components")] But I am getting this error : column "address_components" is of type jsonb[] but expression is of type text[] LINE 1: ... '2018-04-26T07:49:02.101395+00:00'::timestamptz,

How can I automatically add the blogpost author into the member list?

試著忘記壹切 提交于 2021-01-28 08:00:43
问题 Everytime a blogpost is added, a memberlist will be generated. How do I automatically make it such that the author of the post will be included in the member list? I have added the create blog view models.py class BlogPost(models.Model): chief_title = models.CharField(max_length=50, null=False, blank=False, unique=True) body = models.TextField(max_length=5000, null=False, blank=False) members = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="members") author =

Django: how to query parent model based on a filter of a child model?

北战南征 提交于 2021-01-28 07:10:18
问题 I have those classes (simplified for sake of clarity): class Entity(models.Model): adresses = models.ManyToManyField(Address, related_name='persons', through='EntityAddress') class EntityAddress(models.Model): entity = models.ForeignKey(Entity, on_delete=models.CASCADE, blank=False, null=False) address_type = models.ForeignKey(AddressType, models.CASCADE, blank=False, null=False) address = models.ForeignKey(Address, on_delete=models.CASCADE, blank=False, null=False) class Address(models.Model

Django foreign key query, why it returns None?

巧了我就是萌 提交于 2021-01-28 05:23:13
问题 When I try to query foreign keys using get(), I always get None values, even though I know they are set to 1 in the DB. Am I missing something here? Should I do something different to get the foreignkey values? Here's what the code looks like: class Car_to_brand( models.Model ): brand_id = models.ForeignKey( Brand, db_column='brand_id' ) ... class Brand( models.Model ): (id is not defined here but it's the primary key) ... print Car_to_brand.__dict__.get( brand_id ) That would give me {brand

DateTime Field shows invalid format error while passing empty value through form

女生的网名这么多〃 提交于 2021-01-28 04:35:01
问题 I trying to submit form while keeping field for datetime as empty which results in an error showing invalid format for datetime. my modal class lead(models.Model): assigned_to=models.CharField(max_length=15) ref=models.CharField(max_length=10,blank=True) lead_type=models.CharField(max_length=20) priority=models.CharField(max_length=20) hot=models.CharField(max_length=20,blank=True) source=models.CharField(max_length=20,blank=True) mls_lead=models.CharField(max_length=20,blank=True) auto

Getting error : Object of type User is not JSON serializable in django python

元气小坏坏 提交于 2021-01-28 03:38:55
问题 i am new here in django python, when i am trying to get data, from 3 tables, i am getting error Object of type User is not JSON serializable , can anyone please help me why i am getting this error ? here i have added my views.py and models.py file views.py # views.py from tokenize import Token from django.contrib.auth import authenticate from rest_framework import status from rest_framework.exceptions import ValidationError from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_200_OK

How to change class attributes using a method?

自作多情 提交于 2021-01-28 03:18:01
问题 I know this is maybe a dumb question but I haven't found a solution yet. I have a Django model class who has some default attributes, and I want to change the value of the complete variable by calling a function: class Goal(models.Model): text = models.CharField(max_length=500) date = models.DateTimeField(auto_now=True) complete = False def set_complete_true(): complete = True But after calling set_complete_true() the complete variable still False, doesn't change. Thanks in advance! 回答1: An

django rest framework doesn't accept blob picture file (File extension “” is not allowed)

允我心安 提交于 2021-01-28 02:15:46
问题 I am trying to update a User Profile by making a multipart/form-data put request (from my vue frontend using axios) containing a png blob image file. I receive an error message: File extension “” is not allowed. This is the File Field on the Userprofile Model: profile_picture = models.FileField( _("Profile Pictures"), upload_to="profile_picture", max_length=100, blank=True, null=True, ) These are the signals I use in the Userprofile model to save and update the Model. @receiver(post_save,

How do I pull the user profile of the logged in user? django

强颜欢笑 提交于 2021-01-28 02:12:42
问题 I'm using Django's built in User model and this UserProfile model. # this is model for user profile class UserProfile(models.Model): user = models.OneToOneField(User, related_name='profile') city = models.ForeignKey(City) . . . general attributes How can I check if the logged in user has an associated user profile and pass the bool value in the context_dict? I need that to decide whether to show "Create Profile" or "Edit Profile" in the homepage. Thank you. 回答1: If you do not need to fetch

ManyToMany Relationships. Returning fields in def __str__ method

本秂侑毒 提交于 2021-01-27 19:43:14
问题 I have two models: AffectedSegment model class AffectedSegment(models.Model): SEGMENTO_ESCAPULA = 'Escápula' SEGMENTO_HOMBRO = 'Hombro' SEGMENTO_CODO = 'Codo' SEGMENTO_ANTEBRAZO = 'Antebrazo' SEGMENTO_CARPO_MUNECA = 'Carpo/Muñeca' SEGMENTO_MANO = 'Mano' SEGMENT_CHOICES = ( (SEGMENTO_ESCAPULA, u'Escápula'), (SEGMENTO_HOMBRO, u'Hombro'), (SEGMENTO_CODO, u'Codo'), (SEGMENTO_ANTEBRAZO, u'Antebrazo'), (SEGMENTO_CARPO_MUNECA, u'Carpo/Muñeca'), (SEGMENTO_MANO, u'Mano'), ) affected_segment = models