Replies: 1 comment
-
|
@blag You could do this using the instance_of filter on the device models related manager: apple.device_models.instance_of(PhoneModel) # only phone modelsYou might also want to add some convenience functions to your default DeviceModel manager: from polymorphic.query import PolymorphicQuerySet
class DeviceQuerySet(PolymorphicQuerySet)
def phones(self):
return self.instance_of(PhoneModel)
def tablets(self):
return self.instance_of(TabletModel)
class DeviceModel(PolymorphicModel):
objects = DeviceQuerySet.as_manager()
manufacturer = models.ForeignKey(Company, related_name='devices')
apple = Company.objects.get(name='Apple')
apple.devices.all() # All device models, including objects of subclasses
apple.devices.phones() # Only device models that are tablets, using the `TabletModel`'s default manager
apple.devices.tablets() # Only device models that are phones, using the `PhoneModel`'s default manager |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When subclassing from a PolymorphicModel with a relation to another model, it would be awesome if the related model had reverse related descriptors added to it for all subclasses of the polymorphic model.
This is probably better illustrated with an example:
All
Companyobjects have a reverse related descriptor to the polymorphic superclass:but they don't have reverse related descriptors to the subclasses:
It would be super cool if the related class (
Company) had more reverse related descriptors to access subsets of the polymorphic model, like so:This is would make subclassing a polymorphic model a lot more like subclassing an abstract model from vanilla Django.
If there is a known workaround for this I haven't been able to figure it out. Issue #19 looked close but I didn't get it to work since I need
DeviceModelto be a concrete class and I didn't want to have foreign keys toapp_companytable on all of theapp_devicemodel,app_tabletmodel, andapp_phonemodeltables. Any help is greatly appreciated.Beta Was this translation helpful? Give feedback.
All reactions