Hello,
I am relatively new to Django–but seem to pick it up fairly quickly.
I only have this one problem however that I cant resolve and I feel like the solution is right before my eyes, and I can’t seem to spot it.
It may sound elementary to you but I can’t figure this out.
Say in my models.py I have a model–say Post, and a ReadingList model–so a person can create a Post reading list, if you will.
The Post model is written above the ReadingList model. I want the Post model to relate to the reading model but it does not relate it gives me a “Model not defined” error, you the one i’m talking about.
When I place the ReadingList model before the Post it works, but thats not what I need.
I need the Post model to relate to the ReadingList and the ReadingList to relate to Post at the same time so I can then display it in the users profile page.
This loop creates a circular import.
Now, is there another workaround to achieve this?
Or should I just give up Django all together?
I did read the docs about models, I understand how they work, but I can’t figure out how to properly relate all the models the way I want.
Are there other solution to achieve this?
Does this make sense at all? Do I confuse you?
Here is my models.py file
class Post(models.Model):
title = models.CharField(max_length=300,blank=True)
title_original = models.CharField(max_length=300,blank=True)
slug = models.SlugField(max_length=300,blank=True)
authors = models.ManyToManyField(User, null=True,blank=True)
featured_image = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_01 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_02 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_03 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_04 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_05 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
gallery_image_06 = models.ImageField(upload_to='media/FEATURED_IMAGES/',blank=True)
description = RichTextField(max_length=30000,blank=True)
published_date = models.DateTimeField(auto_now_add=False, blank=True)
def get_absolute_url(self):
return reverse(‘blog:Post, args=[self.slug])
def __str__(self):
return self.title
class ReadingList(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=300,blank=True)
slug = models.SlugField(max_length=300,blank=True)
def get_absolute_url(self):
return reverse(‘blog:reading_list', args=[self.slug])
def __str__(self):
return self.title
Now when I try to access the posts via the Post model in my view, it gives me an error because the ReadingList is not related to the Post, there is not ReadingList field in the Post model to filter, does it make sense??
Here is my views.py file
You can see here how I try to access the reading list via the Post model in order to filter the posts, but cant, its not there, and I can’t relate it due to the error.
@login_required
def reading_list(request,reading_list):
rl = get_object_or_404(ReadingList, slug=reading_list)
# whats going on
posts = Post.objects.filter()
return render(request, ‘blog/reading_list.html',{
'rl':rl,
'posts':posts
})