1

I have models as follows:

class Place(models.Model):
    name = models.CharField(max_length=300)  
class Person(models.Model):
    name = models.CharField(max_length=300) 
class Manor(models.Model):
    place = models.ManyToManyField(Place, related_name="place"))  
    lord = models.ManyToManyField(Person, related_name="lord")  
    overlord = models.ManyToManyField(Person, related_name="overlord")

I want to get all the Places attached with the relation 'lord' to a particular person, and then get the centre, using a GeoDjango method. This is as far as I've got:

person = get_object_or_404(Person, namesidx=namesidx)
manors = Manor.objects.filter(lord=person)
places = []
for manor in manors:
    place_queryset = manor.place.all()
    for place in place_queryset: 
        places.append(place)  
if places.collect():
    centre = places.collect().centroid

However, this gives me:

AttributeError at /name/208460/gamal-of-shottle/
'list' object has no attribute 'collect'

Can I either (a) do this in a more elegant way to get a QuerySet of places back directly, or (b) construct a QuerySet rather than a list in my view?

Thanks for your help!

1 Answer 1

1

The way you're doing this, places is a standard list, not a QuerySet, and collect is a method that only exists on GeoDjango QuerySets.

You should be able to do the whole query in one go by following the relations with the double-underscore syntax:

places = Place.objects.filter(manor__lord=person)

Note that your use of related_name="place" on the Manor.place field is very confusing - this is what sets the reverse attribute from Place back to Manor, so it should be called manors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.