Add blog posts without category in django 1.11 - python

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

Related

Django Operational Error: foreign key mismatch

I have two models:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="categories")
name = models.CharField(max_length=30, unique=True, primary_key=True)
class Todo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='todos')
# TODO: Add confirmation before deleting category
category = models.ForeignKey(Category, on_delete=models.CASCADE,
related_name="todos_in_category", null=True)
item = models.CharField(max_length=50)
added = models.DateTimeField(auto_now_add=True)
completed = models.BooleanField(default=False)
Previously, Category's PK was the default id, however, I changed it to the name field. When I ran the migrations, i received the operational error. Thinking that it was perhaps due to a conflict between the existing id fields and the new primary key, I cleared the data in the database but with no success. Any ideas as to what could be the issue here? Thanks!

Creating a unique (attribute) title in a Model

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.

Django admin page not showing user models

My admin page is working fine except when logged in it is not showing any user models. It is hindering my work as I cannot manage users.
I have made custom models as shown below.
Database is MySQL.
models.py
class User(AbstractUser):
is_customer = models.BooleanField(default=False)
is_restaurant = models.BooleanField(default=False)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
food_pref = models.CharField(max_length=10, default='veg')
class Restaurant(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
restaurant_name = models.CharField(max_length=100, blank=False)
Regisrar your models inadmin.py file.
from . models import Model_Name
Then you can register your models in two ways:
I) admin.site.register(Model_Name)
II)
#admin.register(Model_Name)
Class Xyz(admin.ModelAdmin):
pass
Second method gives you more flexibility like list_display, list_filter, date_hierarchy, etc. for customising your Admin section/site.
You can look more about customising admin site at https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#module-django.contrib.admin
Have You registered them in admin.py?
from .models import ModelName
admin.site.register(ModelName)

Django Get object if it has foreignkey

I'm creating an article app (in django) where articles can have images. I want to get articles only when they have at least one image. I already tried:
Article.objects.all().annotate(num_extra=Count("Image")).order_by("-num_extra")
But that only returned a sorted queryset starting with the most images and thats not wat I want.
Is there a way to do that?
My models.py
class Article(models.Model):
id = models.CharField(max_length=8, default=None, primary_key=True, blank=True, verbose_name="ID", unique=True, editable=False)
Category = models.ForeignKey(Category, default=None, on_delete=models.CASCADE, verbose_name="Kategorie")
text = models.CharField(max_length=678543)
#And some other fields
class Image(models.Model):
Article = models.ForeignKey(Article, on_delete=models.CASCADE, default=None, verbose_name="Artikel")
authors = models.ManyToManyField(User, verbose_name="Autor", default=None, blank=True)
#And some other fields
You can filter article by which image is not present at Image
Try this:
Article.objects.filter(image__isnull=False)

How to make django all urls be to level slug?

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

Categories