'Django model location from a view
In my models.py few models related to the different entities
class GuardSecurity(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
etype = models.CharField(max_length=64, default='GuardSecurity', editable=False)
class SecuredFaciliy(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
etype = models.CharField(max_length=64, default='SecuredFaciliy', editable=False)
facility_name=models.CharField(max_length=64)
adress=models.CharField(max_length=64)
class License(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
etype = models.CharField(max_length=64, default='License', editable=False)
license_name=models.CharField(max_length=64)
license_description=models.CharField(max_length=200)
class Equipment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
etype = models.CharField(max_length=64, default='Equipment', editable=False)
equipment_name=models.CharField(max_length=64)
class AllEntity(models.Model):
id = models.UUIDField(primary_key=True)
etype = models.CharField(max_length=64)
class Meta:
managed=False
db_table="entity_full_list"
They all in common have UUID as the primary key.
I implement an SQL view on those models
create view entity_full_list as
select id, etype from entity_equipment
union
select id, etype from entity_guardsecurity
union
select id, etype from entity_license
union
select id, etype from entity_securedfaciliy
To solve the problem of finding a relevant model from a view table I added an etype column that points to the model (string that after can be translated to a command )
Is there another way to point to the exact model from my view?
Solution 1:[1]
You can use string in sql to avoid the extra column.
create view entity_full_list as
select id, 'type' from entity_equipment
union
select id, 'type' from entity_guardsecurity
union
select id, 'type' from entity_license
union
select id, 'type' from entity_securedfaciliy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | irene christodoulou |
