1
votes

We need help

How do I achieve the following...

Here, I have two models example1 and example2 in that I already joined two table using serializers now I want to filter using fields (patient_n_key,centre_master_short_name) either in example2serializer or example2viewsets

models.py

class Example1(models.Model):

    patient_id = models.AutoField(primary_key=True)
    patient_n_key = models.CharField(max_length=15, blank=True, unique=True)
    centre_master_short_name = models.CharField(blank=False, max_length=33)

    class Meta:
        managed = False
        ordering = ['patient_id']
        db_table = 'example1'

class Example2(models.Model):

    appointment_id = models.AutoField(primary_key=True)
    appointment_n_key = models.CharField(max_length=15, blank=True)
    patient_n_key = models.ForeignKey('Example1', 
    db_column='patient_n_key', to_field='patient_n_key', 
    on_delete=models.CASCADE, related_name="Example2_id")
    centre_master_short_name = models.CharField(blank=False, max_length=33)

    class Meta:
        managed = False
        ordering = ['appointment_id']
        db_table = 'example2'

serializers1.py

class Example1serializer(serializers.ModelSerializer):
    class Meta:
        model=models.Example1
        fields='__all__'   

class Example2Serializer(serializers.ModelSerializer):
patient_n_key = Example1serializer(read_only=True)
    class Meta:
        model=models.Example2
        fields='__all__'

views.py

class Example1viewset(viewsets.ModelViewSet):
    queryset=models.Example1.objects.all()
    serializer_class=serializers1.Example1serializer
    lookup_field = 'patient_n_key'

class Example2viewset(viewsets.ModelViewSet):
    queryset=models.Example2.objects.all()
    serializer_class=serializers1.Example2serializer
    lookup_field = 'appointment_n_key'

urls.py

router.register(r'example1', views.Example1viewset, 
base_name='example1')
router.register(r'example2', views.Example2viewset, 
base_name='example2')

How can I filter using viewsets in django rest framework? Or if there is any other prefered way than please list it out. Thank you.

1
Using django_filters this should be possible by simply creating a filter class for your model, then attaching it to your viewset. Take a look at the documentation page for integration with DRF: django-filter.readthedocs.io/en/latest/guide/… - Erik Svedin
It's showing error as 'Example1' object is not iterable - Micheal
Where are you getting that error? Hard to tell exacly whats wrong since im unsure how you've set it up. - Erik Svedin
filter_backends = (DjangoFilterBackend) filter_fields = ('centre_master_short_name','patient_n_key') this is the code in views.py Here only i am getting that error - Micheal
Hi Erik Svedin I have updated the code now can u please check it. - Micheal

1 Answers

0
votes

you can use the django-filtes library for that

class Example1viewset(viewsets.ModelViewSet):
    queryset=models.Example1.objects.all()
    serializer_class=serializers1.Example1serializer
    lookup_field = 'patient_n_key'
    filter_backends = (DjangoFilterBackend)
    filter_fields = ('centre_master_short_name','patient_n_key')