Access RelatedManager for a model created by multi

Summary: Is it possible to access a model sub-classed via multi-table inheritance via a relatedmanager on a related model instance?

Details:

Assuming the following model definitions..

class City(models.Model):
    ...

class Place(models.Model):
    city = models.ForeignKey(City)

class Restaurant(Place):
    ...

I'd like to be able to access all restaurants in the city of Portland like this:

portland = City.objects.get(id=1)
portland.restaurant_set.all()

However restaurant_set or similiar doesn't seem to be an exposed RelatedManager on City , only place_set is exposed.

Is there a way to expose restaurant_set , restaurants , or similar as a related manager when accessing a model instance of City ?

I've also tried created a custom manager for Restaurant, like this:

class RestaurantManager(models.Manager):
    use_for_related_fields = True

class Restaurant(Place):
    objects = RestaurantManager()

However it doesn't seem to get me any closer to what I want.

Apologies in advance as this is just pseudo-code, not the actual models I'm using in my project/app. It's possible I've introduced an error here not in my actual code base, and will happily clarify and update if so.

Also, if it's possible to accomplish this without the use of a 3rd party package (like django-polymorphice etc) I'd prefer that, but if the only reasonable way is to use one of these packages, I will accept that as the answer.

Finally, I'm on Django 1.8 and cannot upgrade for the time being.

链接地址: http://www.djcxy.com/p/25384.html

上一篇: 最简单的方法来测试cuda的存在

下一篇: 访问RelatedManager以创建multi模型