This is my serializer class
class ProjectSerializer(ModelSerializer):
class Meta:
model = Project
exclude = ['deleted_at']
This is Models.py
class MandatoryFields(SoftDeletionModel):
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_created",null=True)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_updated",null=True)
created_date = models.DateTimeField(auto_now_add=True,null=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Project(MandatoryFields, Model):
project_name = models.CharField(max_length=255, blank=True)
project_areas = models.CharField(max_length=255, blank=True)
project_manager = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True,
related_name="%(app_label)s_%(class)s_project_manager")
start_date = models.DateField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
REQUIRED_FIELDS = []
def __str__(self):
return self.project_name
I am getting Id's for the foreign keys created_by,updated_by,project_manager .But I need the values instead of Id.
for example I am getting
created_by : 1
But I need
created_by: Alex
I have tried the following
class ProjectSerializer(ModelSerializer):
created_by = SlugRelatedField(read_only=True, slug_field='created_by')
class Meta:
model = Project
exclude = ['deleted_at']
But I am getting null values.
I think that's achievable by specifying source attribute for serializer field
Somthing like this:
class ProjectSerializer(ModelSerializer):
created_by = CharField(source="created_by.first_name")
class Meta:
model = Project
exclude = ['deleted_at']
Reference : https://www.django-rest-framework.org/api-guide/fields/#source
Related
I am creating a minimal viable product of a recipe application. I would like to leverage the admin site to create a diary entry that, consists of a recipe, that consists of ingredients with amounts and quantities. I read that inlines are possible. However, I have not successfully been able to implement one.
class Meal(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
name = models.CharField(max_length=64, unique=True)
class Meta:
verbose_name = ("Meal")
verbose_name_plural = ("Meals")
def __str__(self):
return f"{self.name}"
class Measurement(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
name = models.CharField(max_length=64, unique=True)
class Meta:
verbose_name = ("Measurement")
verbose_name_plural = ("Measurements")
def __str__(self):
return f"{self.name}"
class Ingredient(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
name = models.CharField(max_length=64, unique=True)
class Meta:
verbose_name = ("Ingredient")
verbose_name_plural = ("Ingredients")
def __str__(self):
return f"{self.name}"
class Recipe(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=64)
class Meta:
verbose_name = ("Recipe")
verbose_name_plural = ("Recipes")
def __str__(self):
return f"{self.name}"
class RecipeIngredient(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
quantity = models.CharField(max_length=64, blank=True, null=True)
measurement = models.ForeignKey(Measurement, on_delete=models.CASCADE, blank=True, null=True)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
class Meta:
verbose_name = ("RecipeIngredient")
verbose_name_plural = ("RecipeIngredients")
class Diary(models.Model):
id = models.BigAutoField(primary_key=True, editable=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
occured_at = models.DateTimeField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
class Meta:
verbose_name = ("Diary")
verbose_name_plural = ("Diaries")
According to your requirement, you can use StackedInline or TabularInline. Here is the code for general StackedInline. TabularInline works in the same way.
#admin.py
class Class_1(admin.StackedInline):
model = Model_1
class Class_2(admin.StackedInline):
model = Model_2
class Class_3(admin.ModelAdmin):
inlines = [Class_1,Model_2]
admin.site.register(Model_Name, Class_3)
I have a models like this:
class Author(models.Model):
name = models.CharField(max_length=150, blank=False, null=False)
dob = models.DateField(null=True, blank=True)
description = models.TextField(max_length=2000, blank=False, default="This author doesn't have any description yet!")
image = models.ImageField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created']
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200, blank=False, null=False)
author = models.CharField(max_length=200)
genres = models.ManyToManyField(Genre, related_name='genre', blank=True)
author = models.ForeignKey(Author, related_name='author', blank=True, on_delete=models.CASCADE)
description = models.TextField(max_length=1200, blank=False, default="This book doesn't have description yet!")
image = models.ImageField(default="")
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created']
def __str__(self):
return self.title
class Review(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
title = models.CharField(max_length=100, null=False, blank=False, help_text="Title overall of your review")
rating = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(5)], help_text='Rating in range 0-5')
description = models.TextField(max_length=1000, null=False, blank=False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
I want to get Book data response in json with my reviews of the book from my Review table but don't know how. I am not getting any useful solution from documentation and Google, please help.
You could set the related_name field in the book field of the Review model.
class Review(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name = 'reviews')
...
Then in the serializer, you can add the related field.
class ReviewSerializer(serializers.ModelSerializer):
class Meta:
model = Review
fields = '__all__'
class BookSerializer(serializers.ModelSerializer):
reviews = ReviewSerializer(many = True)
class Meta:
model = Book
fields = '__all__'
extra_fields = ('reviews', )
I'm creaating an api that user can create a job. when I want to test it with postman and create a job I have this error:
IntegrityError at /job/create/
NOT NULL constraint failed: core_job.category_id
how do i can fix it ?? I'm using generic CreateAPIView
models:
class Category(models.Model):
name = models.CharField(max_length=300)
slug = models.SlugField(max_length=300, unique=True, help_text='write in English.')
sub_category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
class Job(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=400, unique=True)
slug = models.SlugField(max_length=400, unique=True, allow_unicode=True)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
image_1 = models.ImageField(upload_to='products_pic/%Y/%m/%d/', null=True, blank=True)
description = models.TextField(null=True, blank=True)
phone1 = models.CharField(max_length=12, null=True, blank=True)
phone2 = models.CharField(max_length=12, null=True, blank=True)
phase = models.CharField(max_length=1, null=True, blank=True)
address = models.TextField(null=True, blank=True)
daily_start_work_time = models.TimeField(null=True, blank=True)
daily_end_work_time = models.TimeField(null=True, blank=True)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=False)
popular = models.BooleanField(default=False)
views:
class JobCreateView(generics.CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = JobSerializer
queryset = Job.objects.all()
serializers:
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = '__all__'
class JobSerializer(serializers.ModelSerializer):
category = serializers.SerializerMethodField()
class Meta:
model = Job
fields = '__all__'
lookup_field = 'slug'
extra_kwargs = {
'url': {'lookup_field': 'slug'}
}
def get_category(self, obj):
return obj.category.name
The category field is not populating with any value when you create the job. I mean category field is Null when you save that form. I am not sure but any way the problem is related to category field
Problem
I want to create Student With Address. How can I write REST API in Django for same.
Address & Student
class Address(models.Model):
address = models.CharField(max_length=100, null=False, blank=False)
land_mark = models.CharField(max_length=100, null=True, blank=True)
city = models.CharField(max_length=100, null=True, blank=True)
state = models.CharField(max_length=100, null=True, blank=True)
pin_code = models.CharField(max_length=100, null=True, blank=True)
latitude = models.FloatField(default=0.0)
longitude = models.FloatField(default=0.0)
geo_code = models.CharField(max_length=100, null=True, blank=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE,
null=True)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
class Student(models.Model):
name = model.CharField(max_length=100, null=False, blank=False)
address = GenericRelation(Address)
def __str__(self):
return self.name
Serializers
class AddressSerializer(serializers.ModelSerializer):
class Meta:
model = Address
fields = "__all__"
class StuddentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = "__all__"
API View
class AddressApiView(ListCreateAPIView):
queryset = Address.objects.all()
serializer_class = AddressSerializer
class StudentApiView(ListCreateAPIView):
queryset = Student.objects.all()
serializer_class = StuddentSerializer
How do I get my create/view student?
You can try with third-party app rest-framework-generic-relation link
I am having an interesting problem. I am using the ForeignKey call in the relations mananger. I.e. if I want all the objects from a related model known as hamsters the call would be hamsters_set
now here is a working model attached to a serializer everything is working in this implementation.
class SearchCity(models.Model):
city = models.CharField(max_length=200)
class SearchNeighborhood(models.Model):
city = models.ForeignKey(SearchCity, on_delete=models.CASCADE)
neighborhood = models.CharField(max_length=200)
class CityNeighborhoodReadOnlySerializer(serializers.ModelSerializer):
searchneighborhood_set = SearchNeighborhoodSerializer(many=True, read_only=True)
class Meta:
model = SearchCity
fields = ('pk','city','searchneighborhood_set')
read_only_fields =('pk','city', 'searchneighborhood_set')
but with this new model in which I am trying to do the same thing, I am getting an attribute error
class Room(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True, blank=True)
online = models.BooleanField(default=False)
description = models.CharField(max_length=1000, blank=True, null=True)
privateroom = models.BooleanField(default=False)
semiprivateroom = models.BooleanField(default=False)
seatedcapacity = models.CharField(max_length=10, null=True, blank=True)
standingcapacity = models.CharField(max_length=10, null=True, blank=True)
minimumspend = models.PositiveSmallIntegerField(blank=True, null=True)
surroundsoundamenity = models.BooleanField(default=False)
outdoorseatingamenity = models.BooleanField(default=False)
stageamenity = models.BooleanField(default=False)
televisionamenity = models.BooleanField(default=False)
screenprojectoramenity = models.BooleanField(default=False)
naturallightamenity = models.BooleanField(default=False)
wifiamenity = models.BooleanField(default=False)
wheelchairaccessibleamenity = models.BooleanField(default=False)
cocktailseatingseatingoption = models.BooleanField(default=False)
classroomseatingoption = models.BooleanField(default=False)
ushapeseatingoption = models.BooleanField(default=False)
sixtyroundseatingoption = models.BooleanField(default=False)
boardroomseatingoption = models.BooleanField(default=False)
theaterseatingoption = models.BooleanField(default=False)
hallowsquareseatingoption = models.BooleanField(default=False)
class RoomImage(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE)
order = models.PositiveSmallIntegerField(blank=True, null=True)
imageurl = models.CharField(max_length=200, blank=True, null=True)
class RoomAndImageSerializer(serializers.ModelSerializer):
roomimage_set = RoomImageSerializer(many=True)
class Meta:
model = Room
fields = ('name', 'online', 'description','privateroom','semiprivateroom', 'seatedcapacity', 'standingcapacity','minimumspend','surroundsoundamenity','outdoorseatingamenity','stageamenity','televisionamenity','screenprojectoramenity','naturallightamenity','wifiamenity','wheelchairaccessibleamenity','cocktailseatingseatingoption', 'classroomseatingoption','ushapeseatingoption','sixtyroundseatingoption','boardroomseatingoption','theaterseatingoption','hallowsquareseatingoption','roomimage_set')
AttributeError: Got AttributeError when attempting to get a value for
field roomimage_set on serializer RoomAndImageSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance.
Original exception text was: 'QuerySet' object has no attribute 'roomimage_set'.
rather interesting as the two implementations seem to be the same.
Can anyone catch what I am doing wrong?
You need to set your serializer to readonly
class RoomAndImageSerializer(serializers.ModelSerializer):
roomimage_set = RoomImageSerializer(many=True,read_only=True)
class Meta:
model = Room
fields = ('name', 'online', 'description','privateroom','semiprivateroom', 'seatedcapacity', 'standingcapacity','minimumspend','surroundsoundamenity','outdoorseatingamenity','stageamenity','televisionamenity','screenprojectoramenity','naturallightamenity','wifiamenity','wheelchairaccessibleamenity','cocktailseatingseatingoption', 'classroomseatingoption','ushapeseatingoption','sixtyroundseatingoption','boardroomseatingoption','theaterseatingoption','hallowsquareseatingoption','roomimage_set')