django-serializer

How to get all the properties of objects using foreign key in django model

一曲冷凌霜 提交于 2019-12-23 05:42:06
问题 Using Djangorestframework I had created rest api. I have two models in my app countries and states. I had related countries model to states model using Foreign key method, But while fetching list of states in States api i am getting states names, but in the place of country i am getting countries primary key id instead of it's name how can i get all the fields of Countries instead of PK id ---------- Models.py code class countries(models.Model): country = models.CharField(max_length=10) def _

Django Rest Framework: How to pass data to a nested Serializer and create an object only after custom validation

南楼画角 提交于 2019-12-22 12:23:33
问题 I have two models as: class Book(AppModel): title = models.CharField(max_length=255) class Link(AppModel): link = models.CharField(max_length=255) class Page(AppModel): book= models.ForeignKey("Book",related_name="pages",on_delete=models.CASCADE) link = models.ForeignKey("Link", related_name="pages", on_delete=models.CASCADE) page_no = models.IntegerField() text = models.TextField() and serializers class LinkSerializer(serializers.ModelSerializer): class Meta: model = Link fields = ['link']

serialize model object with related objects to JSON

烂漫一生 提交于 2019-12-22 05:11:37
问题 I am having a Person model to store person details. class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthdate = models.DateField() also i am having model PersonLogs to store person's activity logs. class PersonLogs(models.Model): person = models.ForeignKey(Person) time = models.DateTimeField(auto_now_add=True) I am using Django Serializer to return Person objects into JSON format as response. from django.core import

Pass json and deserialize form in django

你离开我真会死。 提交于 2019-12-21 17:45:11
问题 The issue is next: I send some post data with ajax to server. This data looks like: data = { form : $(this).serialize(), some_array: [2,3,4,1] } How to get form object in django? request.POST['form'] returns a string with a form. I'm trying to use import json library. But, when I run value = json.load(request.POST['some_array']) or form = json.load(request.POST['form']) it doesn't work. Printing request.POST['form'] returns the following: u'csrfmiddlewaretoken=I3LWAjfhZRGyd5NS3m9XcJkfklxNhxOR

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

瘦欲@ 提交于 2019-12-20 08:57:28
问题 I want to create a many-to-many relationship where one person can be in many clubs and one club can have many persons. I added the models.py and serializers.py for the following logic but when I try to serialize it in the command prompt, I get the following error - What am I doing wrong here? I don't even have a HyperlinkedIdentityField Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Users\user\corr\lib\site-packages\rest_framework\serializers.py", line 503,

Allowing Edit to editable=False Fields in Django Admin

◇◆丶佛笑我妖孽 提交于 2019-12-19 04:09:36
问题 DRF will use the editable=False on a field to default the Serializer to read-only. This is a very helpful / safe default that I take advantage of (ie I won't forget to set the Serializer to read-only). That being said once I have set editable=False is there any way to then force the Django admin to allow editing one of those fields? Presumably the admin is a super user and I do want him to be able to change the fields value but fore safety I want the default Serializer logic to be read only.

django serialize foreign key objects

半腔热情 提交于 2019-12-18 05:09:10
问题 Serialize django model with foreign key models Serializing Foreign Key objects in Django get foreign key objects in a single query - Django There are couple of question asking for the same thing already. But they are from 2010, and it didn't help me so much. So I figure it maybe been some update to this front since 2010? On google I found this link, which explain usage of natural keys. However my problem concerns of getting foreign objects from django.contrib.auth.models.User so it doesn't

Django / DRF - How can I view a list of all validators for a model / model serializer field?

百般思念 提交于 2019-12-13 18:35:04
问题 This is my UserExtendedSerializer: class UserExtendedSerializer(serializers.ModelSerializer): def __init__(self, *args, **kwargs): super(UserExtendedSerializer, self).__init__(*args, **kwargs) # call the super() for field in self.fields: # iterate over the serializer fields self.fields[field].error_messages['required'] = 'Enter a valid %s.'%field # set the custom error message self.fields[field].error_messages['invalid'] = 'Select a valid %s.'%field # set the custom error message class Meta:

error http status code must be an integer,

孤者浪人 提交于 2019-12-13 03:39:35
问题 I'm trying to serialize my views, and I get this error about http status code must be an integer, I don't see where is the data that must be integer and is passed as string. I will thankful for any help, I'm new in django. my models.py : class User(AbstractUser): """User model.""" username = None email = models.EmailField(_('email address'), unique=True) location = map_fields.AddressField(max_length=200) preferred = models.ManyToManyField(Place, related_name='Preferred') disliked = models

Django Rest Framework custom field only pair of values (without fieldname as dict key)

ⅰ亾dé卋堺 提交于 2019-12-13 01:47:34
问题 Hi i want to provide only pair of values without keys on REST service: take a look on my serializers.py : class TranslationSerializer(serializers.ModelSerializer): translated_term = serializers.CharField(read_only=True) class Meta: model = Translation fields = ('language','translated_term') class VocabSerializer(serializers.ModelSerializer): ... translates = TranslationSerializer(many=True, read_only=True) ... class Meta: model = Vocab fields = ( ..., 'translates',...) The result is the