Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried something similar to Izz ad-Din Ruhulessin's suggestion but it didn't work because I have columns other than the "fake FK" column. The code I tried was:</p> <pre><code>class DynamicPkg(models.Model): @property def cities(self): return City.objects.filter(dpdestinations__dynamic_pkg=self) class DynamicPkgDestination(models.Model): dynamic_pkg = models.ForeignKey(DynamicPkg, related_name='destinations') # Indexed because we will be joining City.code to # DynamicPkgDestination.city_code and we want this to be fast. city_code = models.CharField(max_length=10, db_index=True) class UnmanagedDynamicPkgDestination(models.Model): dynamic_pkg = models.ForeignKey(DynamicPkg, related_name='destinations') city = models.ForeignKey('City', db_column='city_code', to_field='code', related_name='dpdestinations') class Meta: managed = False db_table = DynamicPkgDestination._meta.db_table class City(models.Model): code = models.CharField(max_length=10, unique=True) </code></pre> <p>and the errors I got were:</p> <pre><code>Error: One or more models did not validate: travelbox.dynamicpkgdestination: Accessor for field 'dynamic_pkg' clashes with related field 'DynamicPkg.destinations'. Add a related_name argument to the definition for 'dynamic_pkg'. travelbox.dynamicpkgdestination: Reverse query name for field 'dynamic_pkg' clashes with related field 'DynamicPkg.destinations'. Add a related_name argument to the definition for 'dynamic_pkg'. travelbox.unmanageddynamicpkgdestination: Accessor for field 'dynamic_pkg' clashes with related field 'DynamicPkg.destinations'. Add a related_name argument to the definition for 'dynamic_pkg'. travelbox.unmanageddynamicpkgdestination: Reverse query name for field 'dynamic_pkg' clashes with related field 'DynamicPkg.destinations'. Add a related_name argument to the definition for 'dynamic_pkg'. </code></pre> <p>However I did come up with a working solution by using a proxy model. I did still have to hack around some Django validation that prevents fields from being included in proxy models:</p> <pre><code>class DynamicPkg(models.Model): @property def cities(self): return City.objects.filter(dpdestinations__dynamic_pkg=self) def proxify_model(new_class, base): """ Like putting proxy = True in a model's Meta except it doesn't spoil your fun by raising an error if new_class contains model fields. """ new_class._meta.proxy = True # Next 2 lines are what django.db.models.base.ModelBase.__new__ does when # proxy = True (after it has done its spoil-sport validation ;-) new_class._meta.setup_proxy(base) new_class._meta.concrete_model = base._meta.concrete_model class DynamicPkgDestination(models.Model): dynamic_pkg = models.ForeignKey(DynamicPkg, related_name='destinations') # Indexed because we will be joining City.code to # DynamicPkgDestination.city_code and we want this to be fast. city_code = city_code_field(db_index=True) class ProxyDynamicPkgDestination(DynamicPkgDestination): city = models.ForeignKey('City', db_column='city_code', to_field='code', related_name='dpdestinations') proxify_model(ProxyDynamicPkgDestination, DynamicPkgDestination) class City(models.Model): code = models.CharField(max_length=10, unique=True) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload