Django - Multi Table Inheritance with django admin - django

I am trying to learn django by creating a blog on my own, and I've tried some real simple steps before, but now I want to make something slightly more complex. Currently, I am thinking of dividing the blogs' 'Stories' into 'Blocks'. My idea is to have two child classes 'TextBlock' and 'ImageBlock', and my current models look like this.
class Story(models.Model):
writer = models.CharField(max_length=189)
title = models.CharField(max_length=189)
class Block(models.Model):
story = models.ForeignKey(Story, related_name='+', on_delete=models.CASCADE)
block = EnumField(choices=[
('TXT', "text"),
('IMG', "image"),
])
serial = models.IntegerField(default=0)
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
class TextBlock(Block):
type = models.CharField(max_length=128, blank=True, null=True, default='paragraph')
content = models.TextField(blank=True, null=True)
class ImageBlock(Block):
src = models.URLField()
type = models.CharField(max_length=128, blank=True, null=True, default='full')
title = models.CharField(max_length=189, blank=True, null=True)
photographer = models.CharField(max_length=128, blank=True, null=True)
What I'd like to do now is to create blog entries from the django admin interface. Is it possible to create both types from the main Block? Or do I need to go to both TextBlock and ImageBlock? Any ideas on how I should proceed from here? Thanks.

Related

How to approach file uploads for different models in Django?

Using Django 3 & Python3. What is the right way / best practice to implement this:
Say I have 2 models Project and Task like this:
class Project(models.Model):
name = models.CharField(max_length=120)
class Task(models.Model):
name = models.CharField(max_length=120)
And I need to be able to upload documents 'attached' to any of both models. I am doing:
class BaseDocument(models.Model):
name = models.CharField(max_length=80)
document_date = models.DateField()
def __str__(self):
return self.slug
class Meta:
abstract = True
class ProjectImage(BaseDocument):
image = models.ImageField(upload_to='images/', blank=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_images', blank=True, null=True)
class ProjectDocument(BaseDocument):
raw_file = models.FileField(
upload_to='raw/', blank=True,
storage=RawMediaCloudinaryStorage()
)
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_documents', blank=True, null=True)
class TaskImage(BaseDocument):
image = models.ImageField(upload_to='images/', blank=True)
project = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='task_images', blank=True, null=True)
class TaskDocument(BaseDocument):
raw_file = models.FileField(
upload_to='raw/', blank=True,
storage=RawMediaCloudinaryStorage()
)
project = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='task_documents', blank=True, null=True)
And then creating urls, forms, CBVs for all the models.
This seems inefficient and not very DRY. The question basically is what is the right approach to implement a functionality like this so that the user is able to upload documents to different models.
I've been trying to implement a solution using contenttypes and GenericRelations (anticipating that I may need to attach images to other models in the future) with no success. I was not able to figure out how to implement an 'UploadView' (CreateView) that is able to create the Image/Document object and assign the content_object of the GenericRelation to project/task. I feel that this approach adds a level of complexity that I was not able to handle. If this was the right/best approach, could someone point me out to some tutorial/code sample that implements this?
I hope I am being clear enough. Thanks.

How to create a form in Django from a model with multiple ForeignKey and ManytoManyField fields?

I am a Django newbie. I cannot figure out how to create a form that properly displays my model, which has two ForeignKey fields and three ManytoManyFields. I am familiar with creating forms from more simple models, but I'm stuck on this one. So far, I've tried ModelForm and used ModelChoiceField for ForeignKey relationships, but that did not work. When viewing the form, the fields options did not render. I then tried inlineformset_factory but I could not find helpful examples. Any help is appreciated.
I am trying to create CreateView, UpdateView, and DeleteView options for the model.
models.py
class Animal(models.Model):
name = models.CharField(max_length=500, blank=False, null=False)
**atype = models.ForeignKey(Type, on_delete=models.SET_NULL, blank=False, null=True)**
**ageGroup = models.ForeignKey(AgeGroup, max_length=300, on_delete=models.SET_NULL, blank=False, null=True)**
ageYears = models.PositiveIntegerField(blank=False, null=False)
ageMonths = models.PositiveIntegerField(blank=True, null=True)
sex = models.CharField(max_length=100, choices=SEX, blank=False, null=False, default='NA')
city = models.CharField(max_length=200, blank=True, null=True)
state = models.CharField(max_length=200, blank=True, null=True)
country = models.CharField(max_length=250, blank=True, null=True)
**breedGroup = models.ManyToManyField(BreedGroup, blank=False)**
**breed = models.ManyToManyField(Breed, blank=False)**
tagLine = models.CharField(max_length=300, blank=False, null=False)
goodWithCats = models.BooleanField(blank=False, null=False, default='Not Enough Information')
goodWithDogs = models.BooleanField(null=False, blank=False, default='Not Enough Information')
goodWKids = models.BooleanField(null=False, blank=False, default='Not Enough Information')
profilePic = ResizedImageField(size=[300, 450], quality=100, upload_to='media/', default='', null=True, blank=True, keep_meta=False)
**contact = models.ForeignKey(ContactDetails, on_delete=models.SET_NULL, blank=False, null=True)**
forms.py
class AnimalDetailsForm(ModelForm):
ftype = ModelChoiceField(queryset=Type.objects.all()) #doesn't work
ageGroup = ModelChoiceField(queryset=AgeGroup.objects.all()) #doesn't work
#breed = what method to use?
#breedGroup = what method to use?
class Meta:
model = Animal
exclude = ['ftype', 'ageGroup', 'breed', 'breedGroup']
Also in the Meta of your form you are excluding the fields you are trying to get data for. It should say
class Meta:
model = Animal
fields = ['type', 'ageGroup', 'breed', 'breedGroup']
Docs consulted: https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/
https://docs.djangoproject.com/en/3.1/ref/models/fields/
**edit, removed comment about using type as a variable name.

Django model for different models

I'm learning Django and I'm trying to make a sort of wiki type app. In this wiki there are different type of models: Characters, Items, Spells and Adventures. Each model has some fields that are the same (like name, author, date_created, etc.) and some that are unique to the model, like duration for adventures, alignment for characters, etc. The problem that I have is that if they are different models then every time I want to make something that is common to all of the models (like being able to like, favorite, create, edit, etc.) then I have to code for each model individually. Is there a way of creating a sort of Content model that contains every character, item, spell and adventure so that every time I want to make a form or a function I just make a Content form or Content function?
Here's some of the code, some parts are in spanish but I translated the important parts:
class Character(models.Model):
ALIGNMENT= (
('Legal Bueno', 'Legal Bueno'),
('Legal Neutral', 'Legal Neutral'),
('Legal Malvado', 'Legal Malvado'),
('Neutral Bueno', 'Neutral Bueno'),
('Neutral', 'Neutral'),
('Neutral Malvado', 'Neutral Malvado'),
('Caótico Bueno', 'Caótico Bueno'),
('Caótico Neutral', 'Caótico Neutral'),
('Caótico Malvado', 'Caótico Malvado')
)
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='personajes')
alignment = models.CharField(max_length=50, choices=ALIGNMENT)
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='personaje_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='personaje_favoritos')
class Item(models.Model):
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='items')
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='item_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='item_favoritos')
class Spell(models.Model):
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='hechizos')
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='hechizo_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='hechizo_favoritos')
class Adventure(models.Model):
DURATION = (
('Corta', 'Corta'),
('Mediana', 'Mediana'),
('Larga', 'Larga')
)
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='aventuras')
duration = models.CharField(max_length=50, choices=DURATION)
description = models.CharField(max_length=200)
likes = models.ManyToManyField(Usuario, blank=True, related_name='aventura_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='aventura_favoritos')
I think you can create a base model and then use a OneToOneField to this model in your other models.
class BaseContent(models.Model):
name = models.CharField(max_length=500)
author= models.CharField(max_length=500)
(...)
class Characters(models.Model):
base_content = models.OneToOneField(BaseContent, on_delete=models.CASCADE)
(...)

How to order_by related field date with annotation?

i have this model :
class Node(MPTTModel):
parent = TreeForeignKey('self', on_delete=models.CASCADE, blank=True, null=True, related_name='children')
name = models.TextField(blank=True, null=True)
which have this related model :
class Views(models.Model):
related_tree = models.ForeignKey(Node, on_delete=models.CASCADE, blank=True, null=True, related_name='related_views')
views_count = models.PositiveIntegerField(null=True, blank=True, default=0)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, blank=True, null=True)
view_date = models.DateTimeField(default=timezone.now)
What i am trying to do is to order a queryset of the first model by the view date of the second one, this is the view that i have tried :
last_viewed_trees = Node.objects.filter(tree_type='root').order_by('-related_views__view_date')
In that one the result is correct but i have a duplicate view_date of many users.
I have tried also this one without success :
last_viewed_trees = Node.objects.filter(tree_type='root').annotate(time_of_views= Min('related_views__views_count')
).order_by('-time_of_views')
last_viewed_trees = Node.objects.filter(tree_type='root').annotate(time_of_views= Max('related_views__views_count')
).order_by('-time_of_views')
Nothing have worked for me, there is something that i am missing, but i cannot identify it.

Filter django for objects containing subsets of same many to many relationship

In my database I have user objects with two many to many fields (messages and following) on them that both contain a many to many field related to another object Topic.
class User():
messages = ManyToManyField('Message', related_name='users', blank=True, null=True)
following = ForeignKey('Following', related_name='users', blank=True, null=True)
class Message():
date = DateField(blank=True, null=True)
content = TextField(blank=True, null=True)
topics = ManyToManyField('Topic', related_name='messages', blank=True, null=True)
class Following():
name = CharField(max_length=255, blank=True, null=True)
description = CharField(max_length=255, blank=True, null=True)
topics = ManyToManyField('Topic', related_name='following', blank=True, null=True)
class Topic():
name = CharField(max_length=255, blank=True, null=True)
source = CharField(max_length=255, blank=True, null=True)
I want to filter for all "users" who have "messages" attached to them that do not contain all of the topics attached to the "following" objects on the user.
Right now I am using a loop to accomplish this:
users = set()
for user in User.objects.filter(messages__isnull=False, following__isnull=False).iterator():
if not set(user.following.values_list('topics', flat=True))
).issubset(set(user.messages.values_list('topics', flat=True)):
users.add(user.pk)
Is there a way to accomplish the same thing with a single query?
---- EDIT ----
What I have is this:
User.objects.filter(following__isnull=False
).annotate(following_count=Count('following__topics', distinct=True)
).filter(following__topics__exact=F('message__topics')
).annotate(missing_topics=ExpressionWrapper(
F('following_count') - Count('message__topics', distinct=True),
IntegerField())
).filter(missing_topics__gt=0)
If there is a better way to do this or there are reasons why I should most definitely not do it this way, what are they?
---- EDIT ----
This question helped me to understand and use Håken Lid's answer
This is my new model and my new query:
class User():
messages = ManyToManyField('Message', related_name='users', blank=True, null=True)
following = ManyToManyField('Topic', through='Following', related_name='users', blank=True, null=True)
class Message():
date = DateField(blank=True, null=True)
content = TextField(blank=True, null=True)
topics = ManyToManyField('Topic', related_name='messages', blank=True, null=True)
class Following():
name = CharField(max_length=255, blank=True, null=True)
description = CharField(max_length=255, blank=True, null=True)
user = ForeignKey('User', related_name='following', blank=True, null=True)
topic = ForeignKey('Topic', related_name='following', blank=True, null=True)
class Topic():
name = CharField(max_length=255, blank=True, null=True)
source = CharField(max_length=255, blank=True, null=True)
User.objects.filter(~Q(messages__topics__in=F('following'))
).values('id').annotate(missing_topics=Count('following__topics', distinct=True))
This should be possible using a subquery.
First, make sure Following.topics uses a different related name than Messages.topics.
class Following(models.Model):
topics = ManyToManyField('Topic', related_name='following')
Then it should be possible to create a subquery. Something like this:
from django.db.models import OuterRef, Subquery
user_following_topic = Topic.objects.filter(following__users=OuterRef('pk'))
User.objects.exclude(messages__topics__in=Subquery(user_following_topics.values('pk')))
This might not work and give you your expected output exactly as written, but I think the principle should work for your case as well.
On the other hand, I don't really understand your database structure. It seems you use m2m relations where foreign keys could be more appropriate and simpler. The more complicated your relations are, the harder it is to create this kind of advanced query. And queries with lots of database joins can be very slow, since they might have to process huge amounts of data, compared to simple queries.
For example, instead of using m2m realitions, Following would make more sense to me like so:
class Following():
topic = ForeignKey('Topic', on_delete=models.CASCADE)
user = ForeignKey('User', on_delete=models.CASCADE)
client = models.CharField(max_length=255, blank=True, null=True)
duration = fields.DateRangeField(blank=False, null=False)
So basically a "through" model, as explained in the django docs on model relationships where there's a similar example.