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.