I have below model in my models.py
which has the django User
model as the foreign key and I want to add username
into the filterset_fields
in my views
in order to filter the user employment by username
instead of user id. Any help would be great! Thank you!
class UserEmployment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='employment')
position = models.TextField(blank=True)
company = models.TextField(blank=True)
start_year = models.IntegerField(blank=True, null=True)
end_year = models.IntegerField(blank=True, null=True)
currently_work = models.BooleanField(default=False)
Serializer class,
class UserEmploymentSerializer(serializers.ModelSerializer):
class Meta:
model = UserEmployment
fields = ['id', 'user', 'position', 'company', 'start_year', 'end_year', 'currently_work']
View class,
class UserEmploymentViewSet(viewsets.ModelViewSet):
queryset = UserEmployment.objects.all()
serializer_class = UserEmploymentSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['user'] # I want this to be 'username' in order to filter employment list by username
http_method_names = ['get']
Thank you!