I have this profile model:
class Profile(models.Model):
# Gender
M = 'M'
F = 'F'
O = 'O'
GENDER = [
(M, "Male"),
(F, "Female"),
(O, "Other")
]
# Basic information
background = models.ImageField(upload_to=background_to, null=True, blank=True)
photo = models.ImageField(upload_to=image_to, null=True, blank=True)
slug = AutoSlugField(populate_from=['first_name', 'id', 'gender'])
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100)
birthdate = models.DateField()
gender = models.CharField(max_length=6)
bio = models.TextField(max_length=5000, null=True, blank=True)
languages = ArrayField(models.CharField(max_length=30, null=True, blank=True), null=True, blank=True)
# Location information
website = models.URLField(max_length=256, null=True, blank=True)
# author information
user = models.OneToOneField(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")
class Meta:
verbose_name = "profile"
verbose_name_plural = "profiles"
db_table = "user_profiles"
def __str__(self):
return self.first_name + ' ' + self.last_name
def get_absolute_url(self):
return self.slug
def image_tag(self):
return mark_safe('<img src="/storage/%s" width="50" height="50" />' % (self.photo))
image_tag.short_description = 'Image'
I have this model that represents a location for a user profile:
class Location(models.Model):
name = models.CharField(max_length=50, default=None, null=True, blank=True)
street = models.CharField(max_length=100)
additional = models.CharField(max_length=100)
city = models.OneToOneField(City, on_delete=models.CASCADE, related_name="cities")
zip = models.CharField(max_length=30)
phone = models.CharField(max_length=15)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")
class Meta:
verbose_name = "location"
verbose_name_plural = "locations"
db_table = "locations"
ordering = ["zip"]
def __str__(self):
return self.name
def get_absolute_url(self):
return self.slug
This is the view to get a specific location for a specific profile
class UserLocations(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = LocationSerializer
name = "user-location"
lookup_field = "profile"
def get_queryset(self):
profile = self.kwargs["profile"]
return Location.objects.all().filter(profile=profile)
This is the URL.conf:
urlpatterns_locations = [
path("locations/", LocationsList.as_view()),
path("locations/profiles/<int:profile>", UserLocations.as_view()),
path("locations/countries/", CountriesList.as_view()),
re_path("^locations/states/(?P<country>.+)/$", StatesList.as_view()),
re_path("^locations/cities/(?P<country>.+)/(?P<state>.+)/$", CitiesList.as_view()),
]
Because a profile belongs to a user I would like to get the location for a profile that belongs to a user based on the user ID. The following function does not work and get an error every time:
This is my attempt and changing the lookup_field to user doesn’t work
path("locations/profiles/<int:user>", UserLocations.as_view()),
class UserLocations(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = LocationSerializer
name = "user-location"
lookup_field = "profile"
def get_queryset(self):
user = self.kwargs["user"]
profile = Profile.objects.filter(user=user).values_list("id", flat=True)
return Location.objects.all().filter(profile=profile[0])
ERROR:
Expected view UserLocations to be called with a URL keyword argument named "profile". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
Help is appreciated