Django rest framework get data from foreign key relation? - django

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', )

Related

How to use django-autocomplete-light and django-hacker to make a chained dropdown in django admin?

I have 3 models 1) university 2) courses and 3) enquiry.
My enquiry model has a foreign key to "course". And Course has a foreign key to the university. I only want to show the courses related to that university while adding an enquiry. I tried django-smart-select but failed to do so. I tried This answer but I dont understand the logic and failed to implement in my project.
this is my models.py file
class university(models.Model):
univ_name = models.CharField(max_length=100)
country = CountryField(blank=True, null=True)
univ_desc = models.CharField(max_length=1000)
univ_logo = models.ImageField(upload_to="media")
univ_phone = models.CharField(max_length=10, blank=True)
univ_email = models.EmailField(max_length=254, blank=True)
univ_website = models.URLField(blank=True)
assigned_users = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, default="")
def __str__(self):
return self.univ_name
class Meta:
verbose_name_plural = "Universities"
class Course(models.Model):
university = models.ForeignKey(university, on_delete=models.CASCADE)
course_name = models.CharField(max_length=100)
course_levels = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake = models.ForeignKey(intake, on_delete=models.CASCADE)
documents_required = models.ForeignKey(documents_required, on_delete=models.CASCADE)
course_requirements = models.ForeignKey(course_requirements, on_delete=models.CASCADE)
Active = models.BooleanField()
def __str__(self):
return self.course_name
class enquiry(models.Model):
student_name = models.CharField(max_length=100)
student_phone = models.CharField(max_length=10)
student_email = models.EmailField()
student_address = models.TextField()
current_education = models.ForeignKey(current_education, on_delete=models.CASCADE)
country_interested = CountryField(blank=True, null=True)
university_interested = models.ForeignKey(university, on_delete=models.CASCADE)
course_interested = models.ForeignKey(Course, on_delete=models.CASCADE, limit_choices_to={'Active':True})
level_applying_for = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake_interested = models.ForeignKey(intake, on_delete=models.CASCADE)
assigned_users = models.ForeignKey(User, on_delete=models.CASCADE, default="", limit_choices_to={"is_active": True})
enquiry_status = models.ForeignKey(enquiry_status, on_delete=models.CASCADE, default="")
course_interested= ChainedForeignKey(Course,chained_field= 'university_interested',chained_model_field= 'university',show_all= False,auto_choose= True,sort=True,limit_choices_to = {"Active": True},)
I want to show the course_interested field related to that university. Need help.
I tried using django-smart-select but failed to implement it. I am not aware of jquery and ajax so that is out of the question to use in my project.
got the solution I used the django-select2 and was able to solve this issue.

possible to split the model based on field in DRF admin.py

I have model named organization. I am using this same model model for 2 api's. I have a field code. one API do code auto generation another API takes user entry code. I want to separate the tables based on code. Autogeneration code starts SUB001,SUB002,.... like wise. user entry code thats userwish.
models.py
class Organization(models.Model):
code = models.CharField(max_length=255, null=False, unique=True)
name = models.CharField(max_length=255, null=False)
organization_type = models.CharField(max_length=255, choices=TYPES, null=False, default=COMPANY)
internal_organization = models.BooleanField(null=False, default=True)
location = models.ForeignKey(Location, on_delete=models.RESTRICT)
mol_number = models.CharField(max_length=255, null=True, blank=True)
corporate_id = models.CharField(max_length=255, null=True, blank=True)
corporate_name = models.CharField(max_length=255, null=True, blank=True)
routing_code = models.CharField(max_length=255, null=True, blank=True)
iban = models.CharField(max_length=255, null=True, blank=True)
description = models.TextField(null=True, blank=True)
total_of_visas = models.IntegerField(null=False, default=0)
base_currency = models.ForeignKey(Currency, on_delete=models.RESTRICT, null=True, blank=True, default=None)
logo_filename = models.ImageField(_("Image"), upload_to=upload_to, null=True, blank=True)
def __str__(self):
return self.name
admin.py
#admin.register(Organization)
class OrganizationAdmin(admin.ModelAdmin):
list_display = (
'id',
'code',
'name',
'location',
'organization_type',
'internal_organization',
'mol_number',
'corporate_id',
'corporate_name',
'routing_code',
'iban',
'description',
'total_of_visas',
'base_currency',
'logo_filename',
)
Is there any possible to split models based on code,.. Really Expecting help...
You can use Proxymodel inheritance. Documentation
class AutoGenerationManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(code__istartswith="SUB")
class AutoGeneration(Organization):
objects = AutoGenerationManager()
class Meta:
proxy = True
class UserGenerationManager(models.Manager):
def get_queryset(self):
return super().get_queryset().exclude(code__istartswith="SUB")
class UserGeneration(Organization):
objects = UserGenerationManager()
class Meta:
proxy = True

Generic relation in Django rest

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

Why different records in the model?

I have this abstract models.
class Abstract_Detail(models.Model):
""" Abstract models for detail in subassembly """
name = models.CharField(max_length=50, blank=True, null=True)
mark = models.CharField(max_length=50)
weight_end = models.FloatField(blank=True,
null=True, default=0)
class Meta:
abstract = True
unique_together = ('name', 'makr')
And Details model.
class Detail(Abstract_Detail):
""" Detail """
auto_area = models.FloatField(blank=True, null=True, default=0.0)
size_workpiece = models.CharField(max_length=10, blank=True, null=True)
weight = models.FloatField(blank=True, null=True, default=0)
product = models.ManyToManyField(Products, through='Structure_production')
subassembly = models.ManyToManyField(Subassembly, through='Structure_production')
material = models.ForeignKey(Material, on_delete=models.SET_NULL,
null=True, blank=True)
assortament = models.ForeignKey(Sortment, on_delete=models.SET_NULL,
null=True, blank=True)
class Meta():
unique_together = ('name', 'makr')
What is the problem: in some cases, when you change weight_end in admin Detail, when executing the below code, a new record is created with a combination of the name and mark, which is already in the model.
detail, sost = Detail.objects.get_or_create(
name=name, makr=makr,
defaults={'size_workpiece': size,
'material': material,
'assortament': sortament})
Is there an explanation? Read the documentation fully, the answer is not found.

Django filter intermediary table & social media

So i'm practicing django with a social media website(just for practice). I'm trying to do a filter in my view to get all of the "Beats" from users i'm friends with. Im using an intermediary table for "relationships". I'm currently getting my Beat Stream by using:
my_stream = Beat.objects.filter(artist=user)
But i'm trying to get only the beats of the people i'm "related_to" in the relationships model.
MODELS:
class Beat(models.Model):
created_at = models.DateTimeField( default=datetime.now)
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
likes = models.IntegerField(default=0)
artist = models.ForeignKey(User, null=True, blank=True)
beat_cover = models.ImageField(null=True, blank=True);
admin_name = models.CharField(max_length=255, blank=True, null=True)
class Meta:
ordering = ['-created_at']
def __unicode__(self):
return unicode(self.admin_name)
class UserProfile(models.Model):
user = models.OneToOneField(User)
admin_name = models.CharField(default="beat",max_length=255,blank=True, null=True)
def __unicode__(self):
return unicode(self.admin_name)
class Relationship(models.Model):
from_user = models.ForeignKey(User, related_name="relationships")
to_user = models.ForeignKey(User, related_name="related_to")
class Meta:
index_together = ['from_user','to_user']
You should do:
related_users = Relationship.objects.filter(from_user=user) \
.values_list('to_user', flat=True).distinct()
my_stream = Beat.objects.filter(artist__in=related_users)