Access child model attributes in django templates - python

I have created model inheritance in django models like:
models.py
class Instance(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, related_name='instances')
name = models.CharField(max_length=255, name='name')
slug = AutoSlugField(populate_from='name', unique=True)
created_at = models.DateTimeField(default=timezone.now, editable=False)
type = models.CharField(max_length=255, name='type', default='')
class Meta:
ordering = ['-created_at']
class CustomInstance(Instance):
serverCode = models.TextField(default='', blank=False)
jsonPackageCode = models.TextField(default='', blank=False)
type = models.CharField(max_length=255, name='type', default='custom')
class AwdInstance(Instance):
archive = models.FileField(upload_to='archives', name='archive')
type = models.CharField(max_length=255, name='type', default='awd')
class AwodInstance(Instance):
archive = models.FileField(upload_to='archives', name='archive')
type = models.CharField(max_length=255, name='type', default='awod')
class GithubInstance(Instance):
archive = models.CharField(max_length=255)
type = models.CharField(max_length=255, name='type', default='github')
How can I get child model fields in django templates like user.instances.serverCode?
Help me, please!
Thanks in advance!

Related

Show secondary foreign key values in the inline django admin

models.py
class FarmerAdvisory(BaseModel):
id = models.AutoField(db_column='id', primary_key=True)
title = models.CharField(db_column='title', max_length=200, null=True, blank=True)
description = models.CharField(db_column='description', max_length=750, null=True, blank=True)
farmer_id = models.ForeignKey(Farmer, on_delete=models.CASCADE, null=True, db_column='farmer_id')
class Meta:
verbose_name = 'Farmer Advisory'
verbose_name_plural = 'Farmer Advisories'
managed = True
db_table = 'farmer_advisory'
class ReplyFarmerAdvisory(BaseModel):
reply = models.CharField(db_column='reply', max_length=750, null=True, blank=True)
label = models.CharField(db_column='label', max_length=100, null=True, blank=True)
farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE, db_column='farmer_advisory_id')
objects = models.Manager()
class Meta:
verbose_name = 'Reply Farmer Advisory'
verbose_name_plural = 'Reply Farmer Advisories'
managed = True
db_table = 'reply_farmer_advisory'
class AdvisoryMedia(models.Model):
id = models.AutoField(db_column='id', primary_key=True)
farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE,
db_column='farmer_advisory_id',
null=True, blank=True)
reply_farmer_advisory_id = models.ForeignKey(ReplyFarmerAdvisory, on_delete=models.CASCADE,
db_column='reply_farmer_advisory_id', null=True, blank=True)
farmer_media_file = models.CharField(db_column='farmer_media_file', max_length=50, null=True, blank=True)
is_active = models.BooleanField(db_column='is_active', null=False, default=True)
reply_media_file = models.FileField(upload_to=content_file_name, null=True, blank=True,
db_column='reply_media_file')
class Meta:
verbose_name = 'Advisory Media'
verbose_name_plural = 'Advisories Media'
managed = True
db_table = 'advisory_media'
admin.py
class AdvisoryMediaInline(NestedStackedInline):
model = AdvisoryMedia
extra = 0
class ReplyFarmerAdvisoryInline(NestedStackedInline):
model = ReplyFarmerAdvisory
extra = 1
inlines = [AdvisoryMediaInline]
class FarmerAdvisoryAdmin(NestedModelAdmin):
inlines = [ReplyFarmerAdvisoryInline]
I want to show the fields with respect to the farmer advisory. But when i am adding fk_name = 'farmer_advisory_id' inside AdvisoryMediaInline, i am getting error stating
ValueError: fk_name 'farmer_advisory_id' is not a ForeignKey to
'farmer.ReplyFarmerAdvisory'.
But i want to show the farmer advisory related fields which is not coming by itself.
I think what is happening is the model is looking for values with respect to the to foreign key reply advisory media instead of farmer advisory media.
Please let me know if my question is understandable, if yes please guide me through it.

Django - conditional foreign key

I have the following 4 models in my program - User, Brand, Agency and Creator.
User is a superset of Brand, Agency and Creator.
A user can be a brand, agency or creator. They cannot take on more than one role. Their role is defined by the accountType property. If they are unset (i.e. 0) then no formal connection exists.
How do I express this in my model?
User model
class User(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
email = models.EmailField(max_length=255, null=True, default=None)
password = models.CharField(max_length=255, null=True, default=None)
ACCOUNT_CHOICE_UNSET = 0
ACCOUNT_CHOICE_BRAND = 1
ACCOUNT_CHOICE_CREATOR = 2
ACCOUNT_CHOICE_AGENCY = 3
ACCOUNT_CHOICES = (
(ACCOUNT_CHOICE_UNSET, 'Unset'),
(ACCOUNT_CHOICE_BRAND, 'Brand'),
(ACCOUNT_CHOICE_CREATOR, 'Creator'),
(ACCOUNT_CHOICE_AGENCY, 'Agency'),
)
account_id = models.ForeignKey(Brand)
account_type = models.IntegerField(choices=ACCOUNT_CHOICES, default=ACCOUNT_CHOICE_UNSET)
class Meta:
verbose_name_plural = "Users"
def __str__(self):
return "%s" % self.email
Brand model
class Brand(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
name = models.CharField(max_length=255, null=True, default=None)
brand = models.CharField(max_length=255, null=True, default=None)
email = models.EmailField(max_length=255, null=True, default=None)
phone = models.CharField(max_length=255, null=True, default=None)
website = models.CharField(max_length=255, null=True, default=None)
class Meta:
verbose_name_plural = "Brands"
def __str__(self):
return "%s" % self.brand
Creator model
class Creator(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
first_name = models.CharField(max_length=255, null=True, default=None)
last_name = models.CharField(max_length=255, null=True, default=None)
email = models.EmailField(max_length=255, null=True, default=None)
youtube_channel_username = models.CharField(max_length=255, null=True, default=None)
youtube_channel_url = models.CharField(max_length=255, null=True, default=None)
youtube_channel_title = models.CharField(max_length=255, null=True, default=None)
youtube_channel_description = models.CharField(max_length=255, null=True, default=None)
photo = models.CharField(max_length=255, null=True, default=None)
youtube_channel_start_date = models.CharField(max_length=255, null=True, default=None)
keywords = models.CharField(max_length=255, null=True, default=None)
no_of_subscribers = models.IntegerField(default=0)
no_of_videos = models.IntegerField(default=0)
no_of_views = models.IntegerField(default=0)
no_of_likes = models.IntegerField(default=0)
no_of_dislikes = models.IntegerField(default=0)
location = models.CharField(max_length=255, null=True, default=None)
avg_views = models.IntegerField(default=0)
GENDER_CHOICE_UNSET = 0
GENDER_CHOICE_MALE = 1
GENDER_CHOICE_FEMALE = 2
GENDER_CHOICES = (
(GENDER_CHOICE_UNSET, 'Unset'),
(GENDER_CHOICE_MALE, 'Male'),
(GENDER_CHOICE_FEMALE, 'Female'),
)
gender = models.IntegerField(choices=GENDER_CHOICES, default=GENDER_CHOICE_UNSET)
class Meta:
verbose_name_plural = "Creators"
def __str__(self):
return "%s %s" % (self.first_name,self.last_name)
Agency model
class Agency(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
name = models.CharField(max_length=255, null=True, default=None)
agency = models.CharField(max_length=255, null=True, default=None)
email = models.EmailField(max_length=255, null=True, default=None)
phone = models.CharField(max_length=255, null=True, default=None)
website = models.CharField(max_length=255, null=True, default=None)
class Meta:
verbose_name_plural = "Agencies"
def __str__(self):
return "%s" % self.agency
Update:
So I've whittled it down to this bit here in the model:
ACCOUNT_CHOICE_UNSET = 0
ACCOUNT_CHOICE_BRAND = 1
ACCOUNT_CHOICE_CREATOR = 2
ACCOUNT_CHOICE_AGENCY = 3
ACCOUNT_CHOICES = (
(ACCOUNT_CHOICE_UNSET, 'Unset'),
(ACCOUNT_CHOICE_BRAND, 'Brand'),
(ACCOUNT_CHOICE_CREATOR, 'Creator'),
(ACCOUNT_CHOICE_AGENCY, 'Agency'),
)
account_type = models.IntegerField(choices=ACCOUNT_CHOICES, default=ACCOUNT_CHOICE_UNSET)
limit = models.Q(app_label='api', model='Brand') | \
models.Q(app_label='api', model='Creator') | \
models.Q(app_label='api', model='Agency')
content_type = models.ForeignKey(ContentType, limit_choices_to=get_content_type_choices(), related_name='user_content_type')
content_object = GenericForeignKey('content_type', 'email')
If account_type = 1 then link to brand model
If account_type = 2 then link to creator model
If account_type = 3 then link to agency model
How do I accomplish this? Getting this error:
File "/Users/projects/adsoma-api/api/models.py", line 7, in <module>
class User(models.Model):
File "/Users/projects/adsoma-api/api/models.py", line 28, in User
content_type = models.ForeignKey(ContentType, limit_choices_to=get_content_type_choices(), related_name='user_content_type')
NameError: name 'get_content_type_choices' is not defined
Have you tried exploring Django's GenericForeignKey field?
class User(models.Model):
...
limit = models.Q(app_label='your_app_label', model='brand') |
models.Q(app_label='your_app_label', model='creator') |
models.Q(app_label='your_app_label', model='agency')
content_type = models.ForeignKey(ContentType, limit_choices_to=limit, related_name='user_content_type')
object_id = models.UUIDField()
content_object = GenericForeignKey('content_type', 'object_id')
You can access the User's brand/creator/agency by using the following notation:
User.objects.get(pk=1).content_object
This will be an instance of Brand/Creator/Agency as per the content_type.
https://docs.djangoproject.com/en/stable/ref/contrib/contenttypes/#django.contrib.contenttypes.fields.GenericForeignKey
Update based on your comment
Re 1: Using email:
class User(models.Model):
...
email = models.EmailField(max_length=255, unique=True)
limit = models.Q(app_label='your_app_label', model='brand') |
models.Q(app_label='your_app_label', model='creator') |
models.Q(app_label='your_app_label', model='agency')
content_type = models.ForeignKey(ContentType, limit_choices_to=get_content_type_choices, related_name='user_content_type')
content_object = GenericForeignKey('content_type', 'email')
Note: Email can not be a nullable field anywhere if you follow this approach! Also this approach seems hacky/wrong since the email field is now declared in multiple places; and the value can change if you re-assign the content objects. It is much cleaner to link the GenericForeignKey using the discrete UUIDField on each of the other three models
Re 2: Using account_type field:
ContentType is expected to be a reference to a Django Model; therefore it requires choices that are Models and not integers.
The function of limit_choices_to is to perform a filtering such that all possible models are not surfaced as potential GenericForeignKey
class ContentType(models.Model):
app_label = models.CharField(max_length=100)
model = models.CharField(_('python model class name'), max_length=100)
objects = ContentTypeManager()
However, limit_choices_to does accept callables; so you can write a helper method that translates your account_type to the correct Model
I am not clear about how this transaltion should work; so I leave that to you.
Here is how I solve it, override the save method check your condition
tested on Django 3.2
CUSTOMER = [
('customer', 'customer'),
('supplier', 'supplier'),
]
class Customer(models.Model):
name = models.CharField(max_length=256, null=False, blank=False)
user_type = models.CharField(max_length=32, choices=CUSTOMER)
class SupplierOrder(models.Model):
price = models.FloatField(default=0)
supplier = models.ForeignKey(Customer, related_name='supplier', on_delete=models.CASCADE)
def save(self, *args, **kwargs):
supplier = get_object_or_404(Customer, id=self.supplier.id)
if supplier.user_type != 'supplier':
raise ValueError('selected user must be supplier')
super().save(*args, **kwargs)

How to annotate field by filer from manytomanyfield

my models
class Project(models.Model):
name = models.CharField(max_length=255, unique=True)
info = models.CharField(max_length=255, unique=True)
m2mStatus = models.ManyToManyField("ProjectStatus")
class ProjectStatus(models.Model):
status = models.BooleanField(default=False)
startTime = models.DateTimeField(auto_now=True)
description = models.CharField(max_length=255)
I want to annotate like this:
pj = Project.objects.filter(name="abc")
ps = pj.m2mStatus.last()
Project.objects.filter(name="abc").annotate(status=ps.status)
any help ?

Reference two foreign keys in Django data model

I want to reference two foreign keys in the Conversation model, because user_one and user_two can be a Person or Business either way. What is the best way of expressing this?
class Person(models.Model):
"""
Person model
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
avatar = models.ImageField(upload_to=get_upload_avatar_path, blank=True, null=True, default=None, max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class Business(models.Model):
"""
Business model
` """
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
name = models.CharField(max_length=255, null=True, default=None)
class Conversation(models.Model):
"""
Conversation model
Contains conversation relational data between registered users
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
#user_one = models.ForeignKey(Person, null=True, default=None)
#user_two = models.ForeignKey(Business, null=True, default=None)
class ConversationReply(models.Model):
"""
Conversation reply model
Contains conversation reply data
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
date_time = models.DateTimeField(blank=True, null=True, default=None)
conversation = models.ForeignKey(Conversation, null=True, default=None)
reply = models.CharField(max_length=255)
I would probably use django model inheratance and create an Entity model.
class Entity(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
class Meta:
abstract = True
class Person(Entity):
"""
Person model
"""
avatar = models.ImageField(upload_to=get_upload_avatar_path, blank=True, null=True, default=None, max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class Business(Entity):
"""
Business model
` """
name = models.CharField(max_length=255, null=True, default=None)
class Conversation(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
content_object_1 = GenericForeignKey('content_type', 'object_1_id')
content_object_2 = GenericForeignKey('content_type', 'object_2_id')
Note that due to the use of GenericForeignKey filtering will not work the traditional way. Then you'll be able to do something like this:
from django.contrib.contenttypes.models import ContentType
contenttype_obj = ContentType.objects.get_for_model(person_object)
Conversation.objects.filter(object_id_1=person_object.id, content_type=contenttype_obj)

django model reduce duplicate code

Here is my django model
Because I have ProjectsArchive and StatusArchive for saving history datas
You can see many fields are duplicate
I wonder how to reduce the duplicate code to make the code simple
class Projects(models.Model):
title = models.CharField(max_length=255, blank=True)
link = models.CharField(max_length=300, blank=True)
state = models.CharField(max_length=30, blank=True)
update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
....
class ProjectsArchive(models.Model):
title = models.CharField(max_length=255, blank=True)
link = models.CharField(max_length=300, blank=True)
state = models.CharField(max_length=30, blank=True)
update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
....
class Status(models.Model):
project = models.ForeignKey(Projects, null = True, blank = True)
state = models.CharField(max_length=255, blank=True)
crawl_update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
....
class StatusArchive(models.Model):
project = models.ForeignKey(ProjectsArchive, null = True, blank = True)
state = models.CharField(max_length=255, blank=True)
crawl_update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
....
You can refer here: Abstract base classes
According to your question, I will do:
class BasicInfo(models.Model):
title = models.CharField(max_length=255, blank=True)
link = models.CharField(max_length=300, blank=True)
state = models.CharField(max_length=30, blank=True)
update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
class Meta:
abstract = True
class Projects(BasicInfo):
....
class ProjectsArchive(BasicInfo):
....
After completing BasicInfo, You can reuse the title, link , state and update_at.
However we can create common model containing state and crawl_update_at for Status and StatusArchive.
class StatusInfo(models.Model):
state = models.CharField(max_length=255, blank=True)
crawl_update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
class Meta:
abstract = True
class Status(StatusInfo):
project = models.ForeignKey(Projects, null = True, blank = True)
....
class StatusArchive(StatusInfo):
project = models.ForeignKey(ProjectsArchive, null = True, blank = True)
...
You can create a CommonModel where in you can put your redundant fields then whichever models you want to use them in just inherit CommonModel instead of models.Model.
class CommonModel(models.Model):
class Meta:
abstract = True
title = models.CharField(max_length=255, blank=True)
link = models.CharField(max_length=300, blank=True)
state = models.CharField(max_length=30, blank=True)
update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
class ProjectArchive(CommonModel):
any_new_field_you_want_to_add
What you are looking for is abstract meta class.
https://docs.djangoproject.com/en/1.8/topics/db/models/
class ProjectTemplate(models.Model):
title = models.CharField(max_length=255, blank=True)
link = models.CharField(max_length=300, blank=True)
state = models.CharField(max_length=30, blank=True)
update_at = models.DateTimeField(auto_now=True, blank=True, null=True)
....
class Meta:
abstract = True
class Projects(ProjectTemplate):
pass
class ProjectsArchive(ProjectTemplate):
pass
You could use inheritance.
Please look at the following link
django abstract models versus regular inheritance
And also this the django docs about models (take a look at Abstract base classes)
https://docs.djangoproject.com/en/1.8/topics/db/models/

Categories