Include Specific Foreign Keys in DRF Serializer - django

Here's two simple models to use as an example:
class Author(models.Model):
name = models.CharField(blank=True, max_length=50)
age = models.IntegerField(null=True, )
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author)
date = models.DateField()
Now what I'd like to do, is create a view for Book that pulls in one of the values from Author using the Django Rest Framework. Here's an example ModelSerializer:
class BookMetaSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('title','date','author__name',)
The trouble is that one can't access the fields of a foriegn key in the DRF like I gave above author__name. I haven't been able to figure out how to do this based on the documentation. All help is appreciated, thanks!

You can define author_name field with source argument to get the name of an author.
From the DRF docs on source argument:
The name of the attribute that will be used to populate the field. May
be a method that only takes a self argument, such as
URLField('get_absolute_url'), or may use dotted notation to traverse
attributes, such as EmailField(source='user.email').
Final Code:
class BookMetaSerializer(serializers.ModelSerializer):
# use dotted notation to traverse to 'name' attribute
author_name = serializers.CharField(source='author.name', read_only=True)
class Meta:
model = Book
fields = ('title','date','author_name',)

Related

How to use a serializer for different fields

I am working on a project of blog application in Django Rest Framework. But here I am facing some trouble. At first checkout my code then I will explain the question.
Here is the model.py
class Contact(models.Model):
id_no = models.AutoField(primary_key=True, unique=True)
email = models.EmailField()
name = models.CharField(max_length=1000)
subject = models.CharField(max_length=1000)
description = models.TextField()
And here is the serializer.py
class AddContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = '__all__'
Now in a view function I want to use only email and name field of the Contact model and in another view function I want to use name and description field of that model.
Can I use the same serializer class for different cases?
Please help me.
You can create multiple seralizers for a model. Example
class EmailContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = ['email','name'] # your desired fields here

unique_together and M2M field

I have the following Model:
class CodeSynonyms(models.Model):
code = models.ForeignKey(Codes, on_delete=models.CASCADE)
websites = models.ManyToManyField(Websites)
synonym = models.Charfield(max_length=10)
The idea is that Websites use the Synonyms for specific Codes. One Website can't have few Synonyms for a Code; various Websites can share the same Synonym for specific Code. The following won't work:
class Meta:
unique together = ('code', 'websites')
" 'unique_together' refers to a ManyToManyField 'websites', but ManyToManyFields are not permitted in 'unique_together' "
Is there a way to solve this keeping the M2M relation? It would be handy to have it
you can use through in ManyToManyFieldand connect your many to many relations through custom intermediate table. then add unique_together there:
class CodeSynonyms(models.Model):
# add through field
websites = models.ManyToManyField(Websites, through='WebsiteCode')
synonym = models.Charfield(max_length=10)
class WebsiteCode(models.Model):
code_synonym = models.ForeignKey(CodeSynonyms, on_delete=models.CASCADE)
website = models.ForeignKey(Websites, on_delete=models.CASCADE)
code = models.ForeignKey(Codes, on_delete=models.CASCADE)
class Meta:
unique together = ('code', 'website')

Django rest api: how to return a JsonArray of jsonObjects as a model field?

I have two django models as shown
model 1
class Big(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
model2
class Small(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
address = models.CharField(max_length=200, null=True, blank=True)
big = models.ForeignKey(Big, related_name='small',null=True,on_delete=models.CASCADE)
There can be more than one Small items inside a Big item. The Bigserializer looks like below
class BigSerializer(serializers.ModelSerializer):
class Meta:
model = Hotel
fields = ('name','small')
Now on accessing the Big items,i am getting name and small fields. But the small field returns only the id of the Small model. I need the whole details like name and address of Small item inside the small field. How could i achieve it?
You need to define Small serializer class:
class SmallSerializer(serializers.ModelSerializer):
class Meta:
model = Small
fields = ('name','address')
and use this serializer in BigSerializer class:
class BigSerializer(serializers.ModelSerializer):
small = SmallSerializer(many=True, read_only=True)
class Meta:
model = Hotel
fields = ('name','small')
See details here.
Note that if you need writable nested serialization, you should implement custom create and update methods inside BigSerializer, see section writable nested serialization.

Django models asymmetric relationship

With following Django models:
class Author(models.Model):
name = models.CharField(max_length=30)
bestbookaccordingtome=models.????(Author,null=True, blank=True, default = None)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.OnetoOneField(Author)
I want the classic relationship each book has one author (only).
But I also want to be able to assign a chosen book to author (my favourite book of this author for example).
I tried a foreign key but django didn't like it.
Any python clean way to do it?
What you need is ManyToManyField.
class Author(models.Model):
name = models.CharField(max_length=30)
bestbookaccordingtome = models.ManyToManyField('self', symmetrical=False, related_name='best_book_according_to_me')
Also, if you need to specify extra fields in your n-m model, you can use through to indicate the name of the model.
Hope it helps!

Django 1.2.1 Inline Admin for Many To Many Fields

I recently upgraded to Django 1.2.1 because I was specifically interested in the ability to have basic many-to-many inline fields. When using the admin like so:
Initial models:
class Ingredient(models.Model):
name = models.TextField()
class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient)
Initial admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
exclude = ('ingredients',)
admin.site.register(Recipe,RecipeOptions)
What I got was the same form you would normally see on a ManyToMany field, with some extra rows. Supplying it with extra parameters like an Ingredient ModelForm did not help. Suspecting that something might be wrong with the basic ModelForm associations via model = Foo.manyfields.through, I decided to see if an intermediary model would help. It now displays a working inline form via:
New models:
class RecipeJoin(models.Model):
pass
class Recipe(models.Model):
ingredients = models.ManyToManyField(RecipeJoin,through='Ingredient')
class Ingredient(models.Model):
name = models.TextField()
test = models.ForeignKey(RecipeJoin,null=True,blank=True,editable=False)
New admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeOptions)
Obviously this is not a hack I'd like to use. Anyone know of a way to get a manytomany relationship to display via inline form without either (a) creating an entirely new BasicInline form and template or (b) putting it through an intermediary (or generic admin) model?
TIA. (I apologize for verbosity, it's my first post so wanted to be thorough).
Do one of these examples accomplish what you are trying to do?
a:
# Models:
class Ingredient(models.Model):
name = models.CharField(max_length=128)
class Recipe(models.Model):
name = models.CharField(max_length=128)
ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient')
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
amount = models.CharField(max_length=128)
# Admin:
class RecipeIngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeAdmin(admin.ModelAdmin):
inlines = [RecipeIngredientInline,]
class IngredientAdmin(admin.ModelAdmin):
pass
admin.site.register(Recipe,RecipeAdmin)
admin.site.register(Ingredient, IngredientAdmin)
b:
# Models:
class Recipe(models.Model):
name = models.CharField(max_length=128)
class Ingredient(models.Model):
name = models.CharField(max_length=128)
recipe = models.ForeignKey(Recipe)
# Admin:
class IngredientInline(admin.TabularInline):
model = Ingredient
class RecipeAdmin(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeAdmin)
If I remember correctly (and it's been a while since I've done this part), you need to add the admin for Ingredient and set it to have the custom ModelForm. Then that form will be used in the inline version of Ingredient.