I have two apps news and article which both have exactly the same model name Comment:
class Comment(models.Model):
author = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, default='', blank=True)
body = models.TextField()
post = models.ForeignKey(Photo)
published = models.BooleanField(default=True)
Now, in a view I want to delete certain comments from both apps:
Comment.objects.filter(author=someauthor).delete()
How can I achieve that without changing the model names?
You can use import ... as ... so that both model names do not conflict:
from news.models import Comment as NewsComment
from article.models import Comment as ArticleComment
...
NewsComment.objects.filter(author=someauthor).delete()
ArticleComment.objects.filter(author=someauthor).delete()
Related
I have 3 models as follows:
class Topic(models.Model):
title = models.CharField(max_length=128)
created = models.DateTimeField(auto_now_add=True)
class Thread(models.Model):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Message(models.Model):
thread = models.ForeignKey(Thread, on_delete=models.CASCADE)
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
I want to get:
1- most active topics in this week by number of new messages
2- most active topics by most recent messages
Note: I have solved this challenge by getting the QuerySet and sorting it with python code. I want to know if there is a way to do the same with django ORM.
I have a blog project and users can create posts with similar titles, How can I prevent a user or even the admin from proceeding without getting an error that the title already exists so that I can avoid future errors in the website such as get() returned more than one Post-it returned 2!
I have tried to use class meta for unique together but still, post was saved with the same title
Here is the post model
class Post(models.Model):
designer = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
slug = models.SlugField(blank=True, null=True, max_length=120)
def __str__(self):
return self.title
class Meta:
unique_together = ('title', 'slug')
You can add unique=True
title = models.CharField(max_length=100, unique=True)
See the docs here.
I want to be able to add some blog posts with categories and some without categories in django. with this models django admin won't let me add blog posts without a category. Thanks.
from django.db import models
from django.db.models import permalink
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
pub_date = models.DateField(db_index=True, auto_now_add=True)
# Many-to-one relationship.
category = models.ForeignKey('blog.Category')
class Category(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
Update your model like this:
category = models.ForeignKey('blog.Category', blank=True, null=True)
blank=True allow forms to have a blank value.
null=True allows a null value in the database.
Edit: here is the documentation
How to make django all urls to be top level slug?
Top level slug I mean that all urls has unique slug example:
example.com/articles
example.com/article-1
example.com/article-2
example.com/article-3
example.com/reviews
example.com/reviews-1
example.com/reviews-2
but not:
example.com/articles/article-1
example.com/articles/article-2
example.com/articles/article-3
example.com/reviews/reviews-1
example.com/reviews/reviews-2
I have a lot of apps like articles, reviews, and other custom pages.
So, what you think about this aproach that I create app with model like this:
class Link(models.Model):
slug = models.SlugField(unique=True)
and then I will use it in my articles model like this:
from links.models import Link
class Article(models.Model):
title = models.CharField()
slug = models.OneToOneField(
Link,
on_delete=models.CASCADE,
primary_key=True,
)
body = models.TextField()
.
from links.models import Link
class Review(models.Model):
title = models.CharField()
slug = models.OneToOneField(
Link,
on_delete=models.CASCADE,
primary_key=True,
)
review = models.TextField()
and then I will have only one url field in my mane urls.py file:
url(r'^(?P<slug>[-_\w]+)', views.link, name='link'),
And now how I should filter data where I want to return article or review?
Like this or maybe there exists better solution?
from django.http import HttpResponseRedirect
from .models import Link
from articles.models import Article
from review.models import Review
def link(request, link):
link = Link.objects.get(link=link)
if Article.objects.filter(slug=Link).exists():
link = link.slug
return HttpResponseRedirect(link)
if Review.objects.filter(slug=Link).exists():
link = link.slug
return HttpResponseRedirect(link)
return HttpResponseRedirect('/')
I need that for google because if I decide one day to change /articles to /blog then I will break hundreds of urls in google search.
Your idea is almost perfect. Only changes I recommend:
1) There doesn't seem to be need for a Link model. You slug can be a CharField inside the Article model itself
class Article(models.Model):
title = models.CharField()
slug = models.CharField(max_length=255, unique=True)
body = models.TextField()
2) Reviews belong to articles. So instead of Review having a ForeignKey to this Link object which should no longer exist, it should have a ForeignKey to Article
I have this setup in my models:
class Author(models.Model):
name = models.CharField(max_length=100)
class Topic(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author, null=True, blank=True)
topics = models.ManyToManyField(Topic, null=True, blank=True)
Given an author, I want to know which topics he wrote about:
def author_info(request, pk):
author = get_object_or_404(Author, pk=pk)
topics = ????
If I had specified a through field, I could use that, but now Django makes the through field for me, and since its supposed to be transparent, Id rather not reference the field (unless there is a proper Django construction for that).
Use Lookups that span relationships:
topics = Topic.objects.filter(article__authors=author).distinct()
Note: you have to use distinct here, because the same topic can be selected by different articles.