Django Rest Framework : Setting foreign key value in serializer - python

I have the following models:
class School(models.Model):
id = patch.BigAutoField(primary_key=True)
name = models.CharField('Name', max_length=100)
address = models.CharField('Address', max_length=500, blank=True, null=True)
class Child(BaseModel):
id = patch.BigAutoField(primary_key=True)
name = models.CharField('Name', max_length=100, blank=True, null=True)
school = models.ForeignKey('User', blank=True, null=True, db_constraint=False, db_index=True, on_delete=models.CASCADE, default=None)
I have the following serializers :
class SchoolSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=True, max_length=100)
address = serializers.CharField(required=False, max_length=400)
def create(self, validated_data):
return School.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.address = validated_data.get('address', instance.address)
instance.save()
return instance
class Meta:
model = School
fields = ('id', 'name', 'address')
class ChildSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=False, max_length=100, allow_blank=False)
school = SchoolSerializer()
def create(self, validated_data):
return Child.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.school = validated_data.get('school', instance.school)
instance.save()
return instance
Now the problem that I am facing is that when I saving any value in my child table using serializer then the value of school is showing null in my database but in my request object I am getting value for school_id.

Since this school = SchoolSerializer() school would be a fully serialized object, not scalar value.
Take a look at this example it should help: Writable nested serializer with existing objects using Django Rest Framework 3.2.2
You got to convert serialized object school manually into scalar valued school PK to fill school_id FK field. Or just remove school = SchoolSerializer(), then ChildSerializer will start serializing this field as scalar-valued (but still FK) and thus will make it simply writable directly to the school_id field. The rest of the code should work well.

Related

foreign key in django-import-export

First of all pardon me for my poor English. I'm using django-import-export to upload excel file into my student model that has foreign key relationship with university model
student.. models.py:
class Student(models.Model):
institution = models.ForeignKey(University, on_delete=models.CASCADE)
id = models.CharField(max_length=200, primary_key=True)
first_name = models.CharField(max_length=200)
middle_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
age = models.IntegerField()
faculty = models.CharField( max_length=200, null=True, blank=True)
program = models.CharField( max_length=200, null=True, blank=True)
def __str__(self):
return self.first_name
university.. models.py:
class University(models.Model):
name = models.CharField(max_length=300)
email = models.EmailField(max_length=255, unique=True)
phone_no1 = PhoneNumberField()
phone_no2 = PhoneNumberField(blank=True)
fax_no = PhoneNumberField()
website = models.URLField(max_length=200)
pob = models.IntegerField()
city = models.CharField(max_length=200, blank=True)
logo = models.ImageField(upload_to="logos/", blank=True, null=True)
def __str__(self):
return self.name
After reading django-import-export documentation about ForeignKeyWidget
I edited my resources.py file as following and it works fine when I upload excel file that contain institution id and other student information
resources.py
class StudentResource(resources.ModelResource):
institution = fields.Field(
column_name='institution',
attribute ='institution',
widget = ForeignKeyWidget(University, 'id')
)
class Meta:
model=Student
But I don't want to include institution id into my excel file while I am uploading, because I can find the institution id from Registrar Staff logged in since the RegistrarStaff model has foreign key relationship with university model
class RegistrarStaff(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
university = models.ForeignKey(University, on_delete=models.CASCADE, null=True)
phone_number = models.CharField(max_length=20)
def __str__(self):
return str(self.user)
This is the way I able to find institution id based on whose university Registrar Staff is logged in and passed the value into resource like on number 4 in views.py
views.py:
def upload(request):
if request.method == 'POST':
loged=request.user.id
univ = RegistrarStaff.objects.get(pk=loged).university_id
student_resource = StudentResource(institution=univ)
dataset = Dataset()
new_student = request.FILES['myfile']
if not new_student.name.endswith('xlsx'):
messages.info(request,'Wrong format')
return render(request, 'upload.html')
imported_data = dataset.load(new_student.read(), format='xlsx')
for data in imported_data:
value = Student(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
)
value.save()
return render(request, 'upload.html')
And I intialized it in resources.py as follow
resources.py:
class StudentResource(resources.ModelResource):
def __init__(self, *args, **kwargs):
self._institution=kwargs.pop('institution', None)
super().__init__(*args, **kwargs)
print(self._institution)
class Meta:
model=Student
So, is there anyway I can set the value of institution id = self._institution so as to able to upload excel file that doesn't contain institution id????
You are on the right lines. The way to do this is to pass the institution instance (or id) into the constructor, which is what you have already. All you need to do is to set the institution instance onto the Student model instance before it is saved:
class StudentResource(resources.ModelResource):
def __init__(self, *args, **kwargs):
self._institution=kwargs.pop('institution', None)
super().__init__(*args, **kwargs)
print(self._institution)
def before_save_instance(self, instance, using_transactions, dry_run):
setattr(instance, "institution", self._institution)
class Meta:
model=Student
However, you are bypassing the django-import-export framework by calling Student.save() directly. Don't do this. Use the framework to handle instance creation for you (docs):
result = student_resource.import_data(imported_data, dry_run=True)

Unable to POST JSON data from multiple select element with Django REST Framework

I would like to be able to send an AJAX POST request to my API endpoint to create a new instance of my Asset model with multiple Category instances referenced in my Asset model, hence the many-to-many field type in my Asset model.
I'm able to successfully POST and create new Asset instances, however my category field won't accept any data at all. The category field remains empty when a new Asset instance is created. I think it has something to do with my CategorySerializer. I'm still learning how to use Django REST Framework so I'd appreciate if I could get some help figuring out how to work with serializers in Django REST Framework.
I've already tried modifying the AssetSerializer create method to handle parsing the JSON and validating the data but that hasn't worked. I've also tried other solutions suggested in other posts I've found on StackOverflow but haven't found anything that works for my situation.
Here's my serializers.py file:
class CategorySerializer(serializers.ModelSerializer):
name = serializers.CharField(required=False, read_only=True)
class Meta:
model = Category
fields = ('id', 'name')
class AssetSerializer(serializers.ModelSerializer):
name = serializers.CharField(allow_null=True)
description = serializers.CharField(allow_null=True)
manufacturer = serializers.CharField(allow_null=True)
uid = serializers.UUIDField(read_only=True, allow_null=True)
borrower = BorrowerSerializer(allow_null=True, read_only=True)
condition = serializers.ChoiceField(choices=Asset.CONDITION_TYPE, default='g', allow_null=True)
owner = serializers.ReadOnlyField(source='owner.username')
return_date = serializers.DateField(allow_null=True)
checked_out = serializers.BooleanField(allow_null=True)
category = CategorySerializer(required=False, many=True)
class Meta:
model = Asset
fields = ('uid',
'name',
'manufacturer',
'model',
'description',
'owner',
'condition',
'category',
'borrower',
'checked_out',
'return_date',
'is_dueback',
)
def update(self, instance, validated_data):
instance.borrower = validated_data.get('borrower', instance.borrower)
instance.return_date = validated_data.get('return_date', instance.return_date)
instance.checked_out = validated_data.get('checked_out', instance.checked_out)
instance.name = validated_data.get('name', instance.name)
instance.manufacturer = validated_data.get('manufacturer', instance.manufacturer)
instance.model = validated_data.get('model', instance.model)
instance.description = validated_data.get('description', instance.description)
instance.condition = validated_data.get('condition', instance.condition)
instance.category = validated_data.get('category', instance.category)
instance.save()
return instance
def create(self, validated_data):
return Asset.objects.create(**validated_data)
Here's my Asset model:
class Asset(models.Model):
"""Model representing an Asset"""
uid = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=200)
manufacturer = models.CharField(max_length=64)
model = models.CharField(max_length=128)
description = models.TextField()
category = models.ManyToManyField(Category)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
borrower = models.ForeignKey(Borrower, on_delete=models.CASCADE, null=True, blank=True)
checked_out = models.BooleanField(default=False)
return_date = models.DateField(null=True, blank=True)
CONDITION_TYPE = (
('e', 'Excellent'),
('g', 'Good'),
('f', 'Fair'),
('p', 'Poor'),
)
condition = models.CharField(
max_length=1,
choices=CONDITION_TYPE,
blank=True,
help_text='Asset condition')
class Meta:
ordering = ['return_date']
#property
def is_dueback(self):
if self.return_date and date.today() > self.return_date:
return True
return False
def display_category(self):
"""Create a string for the Category. This is required to display category in Admin."""
return ', '.join(category.name for category in self.category.all())
display_category.short_description = 'Category'
def __str__(self):
return f'{self.uid} - {self.name}'
def get_absolute_url(self):
return reverse('asset-detail', args=[str(self.uid)])
Here's my Category model:
class Category(models.Model):
"""Model representing an Asset category"""
name = models.CharField(max_length=128)
def __str__(self):
return self.name
I'd appreciate any help you could provide. Thank you in advance.
i'm almost new in DRF but i try to help. why you writing all the field in serializer when you using ModelsSerializer? not need to telling ModelSerializer what type of field should be because you are pointing to model in class Meta and DRF know about fields and type and etc . second about allow_null=True in serializer, when Model haven't null=True you can't except DRF can create a not null-able field for instance with null=True so if you wnt a field can be null just add null=True in Model class . for your problem about ManytoMantry field try to use Primary key relation for ManyToMany fields in your serializers then pass id of Category instances in list:
class AssetSerializer(serializers.ModelSerializer):
borrower = BorrowerSerializer(allow_null=True, read_only=True)
category = serializers.PrimaryKeyRelatedField(many=True, queryset=Category.objects.all())
class Meta:
model = Asset
fields = ('uid',
'name',
'manufacturer',
'model',
'description',
'owner',
'condition',
'category',
'borrower',
'checked_out',
'return_date',
'is_dueback',
)
read_only_fields = ( 'uid' , ) # this fields will be read_only
depending on how you using this serializer in your view for save and update have difference way. if your view is generics class so will do create and update itself by POST and PUT method .and for other class view that isn't belong to generics DRF view you can using serializer.save() to create a new instance.wish help you.
pass data something like:
{
"name" : "foo",
"manufacture" : "foo",
.
.
.
"category" : [1,2,3,24,65]
}

Many to Many model with a dropdown in Django Rest Framework?

I am trying to create a Many to Many relation with a model in between, I have a Client model, and a Zone model, each client may have access to different zones, and each zone may have multiple clients.
Therefore I created a model called Access Permission, that stores said relation, and I want to show a dropdown selector in the post form that shows the existing clients and zones, or to ask for the Id of an existing object, instead of showing the form to create new ones.
These are my models:
class Zone(models.Model):
name = models.TextField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class Client(models.Model):
name = models.TextField()
birthDate = models.DateField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class AccessPermission(models.Model):
idClient = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
idZone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
And these my current serializers:
class ZoneSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Zone
fields = ('name',)
class ClientSerializer(serializers.HyperlinkedModelSerializer):
zones = ZonesSerializer(source='accesspermission_set', many=True, read_only=True)
class Meta:
model = Client
fields = ('name', 'birthDate', 'zones')
class AccessPermissionSerializer(serializers.ManyRelatedField):
idClient = ClientSerializer(many=False)
idZone = ZoneSerializer(many=False)
class Meta:
model = AccessPermission
fields = ('idClient', 'idZone')
Is there any way to ask for the Id of an existing object, or show the existing ones, instead of the fields to create new ones?
You can do it like:
models
class AccessPermission(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
zone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
serializers
class AccessPermissionSerializer(serializers.ManyRelatedField):
id = serializers.IntegerField(read_only=True)
client_id = serializers.PrimaryKeyRelatedField(
queryset=Client.objects.all(), source='client', allow_null=False, required=True
)
zone_id = serializers.PrimaryKeyRelatedField(
queryset=Zone.objects.all(), source='zone', allow_null=False, required=True
)
class Meta:
model = AccessPermission
fields = (
'id', 'client_id', 'zone_id'
)

Django Restframework N to N Table

I have two classes in my model.py:
class Table(models.Model):
name = models.CharField(max_length=100, blank=True)
class Activity(models.Model):
name = models.CharField(max_length=100, blank=False)
process = models.CharField(max_length=100, blank=True, default='')
For these two classes I created a serializer each:
class TableSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=True, allow_blank=False, max_length=100)
def create(self, validated_data):
return Table.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.save()
return instance
(same for Activity)
I now want to store the relation between table and activity. An activity can have multiple tables. Now I don't know if I need to create a new class like this:
class TableActivity(models.Model):
activtiy = models.ManyToManyField('Activity')
table = models.ManyToManyField('Table')
Or that this can be included into the activity already?
How should my model/serializer look like then?
You shouldn't create class TableActivity. You can add field activtiy = models.ManyToManyField('Activity') to table Table or add field table = models.ManyToManyField('Table') to table Activity. And you should use model serializer for creating object in database

Custom behaviour of RelatedField while Serialization

I have two model below with foreign key relation.
class City(TimeStampedModel):
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
long_name = models.CharField(max_length=200)
short_name = models.CharField(max_length=200)
class Address(TimeStampedModel):
address_object = GenericForeignKey('address_content_type', 'object_id')
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200, null=True)
landmark = models.CharField(max_length=200, null=True)
city = models.ForeignKey(City, related_name='address_city')
And I have defined Below serializer for Address
class CityRelation(serializers.RelatedField):
def to_representation(self, value):
if isinstance(value, City):
return CitySerializer(value).data
class AddressBookSerializer(serializers.ModelSerializer):
city = CityRelation(read_only=True)
class Meta:
model = Address
fields = ('id', 'uuid', 'address1', 'address2', 'landmark', 'city')
#atomic
def create(self, validated_data):
address_book = Address(**validated_data)
address_book.save()
return address_book
def update(self, instance, validated_data):
instance.address1 = validated_data['address1']
instance.address2 = validated_data['address2']
instance.landmark = validated_data['landmark']
instance.city = validated_data['city']
instance.save()
return instance
Here While Deserializing i want to pass only city_id in JSON but while serializing i want complete city object....so i override relatedfield...but i am getting error in deserialization. I also tried to override to_internal_value() method but it did not called during deserialization. How can i do that??
I solved it...Actually i set readonly=True for city relation but with readonly only to_representation() get called not to_internal_value()...so i passed queryset...

Categories

Resources