How to search with django_tables2 when using filters - filter

My problem is that I can not take back any data after filtering.
My search fields are : id name and surname. The two last taken from the Client which is foreign key.
filters.py
class OrderFilter(django_filters.FilterSet):
client__name = django_filters.CharFilter(lookup_expr='icontains')
client__surname = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Order
fields = ['id']
models.py
class Order(models.Model):
client = models.ForeignKey(Client,verbose_name=u'Client')
tables.py
class OrderTable(tables.Table):
#CUSTOM COLUMN EXAMPLE
order_id=tables.Column(verbose_name= 'ID Order',orderable=False,empty_values=())
class Meta:
#define the model
model = Order
exclude=('id')
template_name = 'django_tables2/bootstrap.html'
sequence = ("order_id")
views.py
class OrderListView(SingleTableMixin,FilterView):
table_class = OrderTable
model = Order
template_name='orders/orde_list.html'
filterset_class = OrderFilter
def get_context_data(self, **kwargs):
context = super(OrderListView, self).get_context_data(**kwargs)
##filter the orders of the past 4 months.
last_four_months=date.today() - timedelta(120)
object_list=Order.objects.filter(order_created__gte=last_four_months,ays=1).order_by('-invoice_date')
table=self.table_class(object_list)
RequestConfig(self.request).configure(table)
context['table'] = table
#clear all fields
has_filter = any(field in self.request.GET for field in set(self.filterset_class.get_fields()))
context['has_filter'] = has_filter
return context
I noticed that when I create a custom queryset to fill my table, for ex: object_list=Order.objects.filter(order_created__gte=last_four_months).order_by('-invoice_date') the filtering is not working.
When I put nothing it works properly.
Any idea why this happening?

The issue was that I have not defined that my custom queryset must be inserted in the custom table(tables.py) and more important to be filtered by the custom filter (filters.py).
After doing that the custom queryset filtered correctly.
class OrderAYSListView(LoginRequiredMixin,SingleTableMixin,FilterView):
login_url = '/login/'
table_class = OrderTable2
model = Order
template_name='orders/order_ays_list.html'
filterset_class = OrderFilter
def get_context_data(self, **kwargs):
context = super(OrderAYSListView, self).get_context_data(**kwargs)
##filter the orders of the past 4 months.
last_four_months=date.today() - timedelta(120)
object_list=Order.objects.filter(order_created__gte=last_four_months,ays=1).order_by('-invoice_date')
f = self.filterset_class(self.request.GET, queryset=object_list)
context['filter'] = f
table = self.table_class(f.qs)
RequestConfig(self.request).configure(table)
context['table'] = table
#clear all fields
has_filter = any(field in self.request.GET for field in set(self.filterset_class.get_fields()))
context['has_filter'] = has_filter
return context

Related

Retrieving a Many-t-Many Field value with django-rest-framework serializers

I want to retrieve manytomany field value in my serializer.
My Project Model is as
class Project(models.Model):
title = models.CharField(max_length=200,blank=True)
detail = models.CharField(max_length=200,blank=True)
addedby = models.ForeignKey(User,on_delete=models.CASCADE,related_name='projectadder')
date = models.DateTimeField(auto_now_add=True)
projecttype = models.ForeignKey(ProjectType,on_delete=models.CASCADE,related_name='projecttpye')
cost = models.CharField(max_length=20,blank=True)
pros = models.CharField(max_length=500,blank=True)
cons = models.CharField(max_length=500,blank=True)
team = models.ManyToManyField(User)
supervisor = models.ManyToManyField(Supervisor)
cosupervisor = models.ManyToManyField(CoSupervisor)
technology = models.ManyToManyField(Technology)
tags = models.ManyToManyField(Tag)
teamleader = models.ForeignKey(User,on_delete=models.CASCADE,related_name='teamleader')
proposal = models.CharField(max_length=500,blank=True)
buyers = models.ManyToManyField(Buyer)
reviews = models.ManyToManyField(Review)
requirements = models.ManyToManyField(ProjectRequirement)
images = models.ManyToManyField(ProjectImage)
My serializer is as:
class ProjectSerializer(serializers.ModelSerializer):
projecttype = serializers.ReadOnlyField(source='projecttype.name')
addedby = serializers.ReadOnlyField(source='addedby.username')
teamleader = serializers.ReadOnlyField(source='teamleader.username')
# team = serializers.ReadOnlyField(source='team')
class Meta:
model= Project
fields = '__all__'
It is retrieving ForiegnKey elements sucessfuly but not retriving ManytoMany Field Values?
I think you need to set the serializers for those ManyToManyFields.
For example, the model has a many to many relationship with the Supervisor model.
So you can set the supervisors field in the ProjectSerializer.
class ProjectSerializer(serializers.ModelSerializer):
...
supervisors = SupervisorSerializer(many = True, read_only = True, fields=(...))
class Meta:
model= Project
fields = (..., 'supervisors',)
In order to set fields parameters, you can customize __init__ function.
class SupervisorSerializer(serializers.ModelSerializer):
...
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
set_fields = kwargs.pop('fields', None)
# Instantiate the superclass normally
super().__init__(*args, **kwargs)
if set_fields is not None:
allowed = set(set_fields )
existing = set(self.fields)
for field_name in existing - allowed:
self.fields.pop(field_name)
You need to add the serializers for all the many to many fields.
Hope it could help.

How to post data to Embedded document with Mongoengine REST

I am trying to use Django with mongoengine to make an API.
So far I can get the objects and delete them. but when I want to post some data. Lets say student + course it is giving an error:
type object 'Course' has no attribute 'objects'
Models en ..
#Model.py
class Course(EmbeddedDocument):
course_name = StringField(max_length=200)
course_fee = StringField(max_length=200)
class Student(Document):
student_name = StringField(max_length=200)
student_contactperson = StringField(max_length=200)
student_adress = StringField(max_length=200)
courses = ListField(EmbeddedDocumentField(Course))
#Serializers.py
class CourseSerializer(EmbeddedDocumentSerializer):
class Meta:
model = Course
fields = ('course_name','course_fee')
class StudentSerializer(DocumentSerializer):
courses = CourseSerializer(many=True)
class Meta:
model = Student
fields = ('student_name','student_contactperson','student_adress','courses')
depth = 2
def create(self, validated_data):
course_data = validated_data.pop('courses')
student = Student.objects.create(**validated_data)
Course.objects.create(student=student, **course_data)
return student
#Views.py
class StudentViewSet(meviewsets.ModelViewSet):
lookup_field = 'name'
queryset = Student.objects.all().order_by('-date_joined')
serializer_class = StudentSerializer
A Document represents a MongoDB document (i.e a record in a collection), a Document class is bound to a particular collection. An EmbeddedDocument represents a structure that gets nested in a Document.
So by design an EmbeddedDocument isn't attached to any collection unless you embed it inside a Document.
This means that you can't query or save an EmbeddedDocument class, you need to query/save the parent Document.
Document.objects is an entry point for querying a collection, it only exists on Document classes. You are calling Course.objects.create but Course is an EmbeddedDocument.
I believe you need to change your code to the following
class StudentSerializer(DocumentSerializer):
...
def create(self, validated_data):
course_data = validated_data.pop('courses')
course = Course(**course_data) # assuming course_data is {course_name: ..., course_fee: ...}
return Student.objects.create(courses=[course], **validated_data)

Django DRF serializer - inserting data containing foreign key relationships

I have the following models:
class Contact(models.Model):
class Meta:
managed = False
db_table = 'contact'
class ContactPhone(models.Model):
contact = models.ForeignKey(Contact, on_delete = models.CASCADE)
number = models.CharField(max_length = 45)
class Meta:
managed = False
db_table = 'contact_phone'
Also, I have the following serializers:
class ContactSerializer(serializers.ModelSerializer):
server_id = serializers.IntegerField(source='id', read_only=True)
class Meta:
model = Contact
fields = '__all__'
class ContactPhoneSerializer(serializers.ModelSerializer):
class Meta:
model = ContactPhone
fields = '__all__'
Now, I have a view that insert phone numbers for an existing contact.
The input is a json that looks like this:
data = {'contact_id': 12322,
'phones':[{'number': '89120000001'}]}
The view:
def insert_contact_phone(request):
for record in request.data['phones']:
data['contact_id'] = request.data['contact_id']
serializer = ContactPhoneSerializer(data = data)
if serializer.is_valid():
serializer.save()
I end up with the following error:
RelatedObjectDoesNotExist at /contacts/edit ContactPhone has no
contact.
What am I doing wrong?
If you specify __all__ for the fields in your ContactPhoneSerializer, it does not include contact_id.
So the contact_id taken from the json input is not serialized. It is basically ignored and when you try to save and create new ContactPhone - it fails, because it does not have contact's foreign key correctly set.
But simply adding contact_id to the serializer's fields won't solve your problem.
In your view, i recommend you to set the contact instead:
data['contact'] = request.data['contact_id']
and pass this to the ContactPhoneSerializer.

How can i add #property field to django rest serializer with '__all__'

I have model
class A(models.Model):
is_enable = models.BooleanField(default=False)
title = models.CharField(max_length=255, blank=True, null=True)
updated_date = models.DateTimeField(auto_now=True)
show_count = models.IntegerField(default=0)
answers_count = models.IntegerField(default=0)
audience = JSONField()
events = JSONField()
rules = JSONField()
message = models.TextField(blank=True)
#property
def conversion(self):
if self.show_count == 0:
return 0.0
return (self.answers_count / self.show_count) * 100
And i have serializer
class ASerializer(serializers.ModelSerializer):
audience = serializers.JSONField()
events = serializers.JSONField()
rules = serializers.JSONField()
class Meta:
model = Trigger
fields = '__all__'
I want to add to response #property conversion` field
and I want to do something like this in serializer
class Meta:
model = Trigger
fields = '__all__' + conversion
I know that i can make something like this
class Meta:
model = Trigger
fields = ('is_enable', 'title' ... 'conversion')
But i want add all fields and conversion field and do this more beautiful
you can use somethig like this:
class ModelMixin:
#classmethod
def _get_model_fields(cls):
all_fields = cls._meta.get_fields()
fields = [i.name for i in all_fields if i.__class__.__name__ not in ['ManyToManyRel', 'GenericRelation', 'ManyToOneRel']]
return fields
#classmethod
def get_serializer_fields(cls):
fields = cls._get_model_fields()
return fields
class A(ModelMixin, models.Model):
...
class ASerializer(ModelSerializer):
custom_field = serializers.JSONField()
class Meta:
model = A
fields = A.get_serializer_fields()
fields.append('cusom_field')
Might still not be as clean as you want it to be, but using <model>._meta.fields you can get all fields of that model, and using the name property of each field you can retrieve the names that refer to them.
As the serializers file is interpreted as python, it supports code execution, meaning you can use all python functionalities when setting properties, including a for loop, which gives us the ability to do this:
fields = (tuple((f.name for f in A._meta.fields)) + ('conversion',))
Needed this for my own model as well and there it seemed to work, so hopefully so does it for you.
You can create a SerializerMethodField in your serializer and later define a method in serializer itself that can call your functions you defined in models using #property
class ASerializer(serializers.ModelSerializer):
audience = serializers.JSONField()
events = serializers.JSONField()
rules = serializers.JSONField()
conversion = serializers.SerializerMethodField()
class Meta:
model = Trigger
fields = '__all__'
def get_conversion(self,obj):
return obj.conversion
This should allow you to keep your code beautiful without modifying much of your code.

Django REST Framework - optimizing nested serialization of a queryset

I have a model and serializer like this:
models.py
class CalendarEvent(BaseMixin):
title = models.TextField(blank=True, null=True)
class CalendarEventReminder(BaseMixin):
event = models.ForeignKey(CalendarEvent, related_name = 'reminders')
minutes = models.CharField()
class Meta:
managed = False
db_table = 'calendar_event_reminder'
def __str__(self):
return self.minutes
serializer.py
class CalendarEventSerializer(serializers.ModelSerializer):
reminders = serializers.StringRelatedField(many=True)
class Meta:
model = CalendarEvent
fields = ('title', 'reminders')
In my view, I do the following:
def test(request):
#...
event = CalendarEvent.objects.filter(id__in = [930, 935])
serializer = CalendarEventSerializer(event, many = True)
print (serializer.data)
#...
When I open the Debug Toolbar, I see that the database hits reminders table twice for each of the calendar events.
The question is, how this behavior could be optimized.
The most straight-forward way would be prefetching the CalendarEventReminders of the CalendarEvents in your view:
# views.py
def get(request):
event = CalendarEvent.objects.filter(id__in = [930, 935]) \
.prefetch_related('reminders')
# ...
This will prefetch all CalendarEventReminders while getting the CalendarEvents.
Note that this will not trigger a sql join like select_related() would do. We can't use select_related() in this case because we're following the relation backwards. :)
Check out the Django Docs regarding prefetch_related.

Resources