Made the slug variable for futures urls and did ./makemigrations and migrate and the parameter appears in the admin panel, but when I try to migrate after I made an empty "theblog" migration I get this error:
class Migration(migrations.Migration):
File "xxx/theblog/models.py", line 104, in Migration
migrations.RunPython(generate_slugs_for_old_posts, reverse=reverse_func),
TypeError: __init__() got an unexpected keyword argument 'reverse'
changed the slug parameter from null and blank to unique but that doesn't seem to be the problem now. I know that the problem is coming from get_success_url but really don't know how to fix it.
models.py:
from django.utils.text import slugify
class Post(models.Model):
title= models.CharField(max_length=100)
header_image = models.ImageField(null=True , blank=True, upload_to="images/")
title_tag= models.CharField(max_length=100)
author= models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextUploadingField(extra_plugins=
['youtube', 'codesnippet'], external_plugin_resources= [('youtube','/static/ckeditor/youtube/','plugin.js'), ('codesnippet','/static/ckeditor/codesnippet/','plugin.js')])
post_date = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=50, default='uncategorized')
slug = models.SlugField(unique=True)
snippet = models.CharField(max_length=200)
status = models.IntegerField(choices=STATUS, default=0)
likes = models.ManyToManyField(User, blank=True, related_name='blog_posts')
def save(self, *args, **kwargs):
self.slug = self.generate_slug()
return super().save(*args, **kwargs)
def generate_slug(self, save_to_obj=False, add_random_suffix=True):
generated_slug = slugify(self.title)
random_suffix = ""
if add_random_suffix:
random_suffix = ''.join([
random.choice(string.ascii_letters + string.digits)
for i in range(5)
])
generated_slug += '-%s' % random_suffix
if save_to_obj:
self.slug = generated_slug
self.save(update_fields=['slug'])
return generated_slug
def generate_slugs_for_old_posts(apps, schema_editor):
Post = apps.get_model("theblog", "Post")
for post in Post.objects.all():
post.slug = slugify(post.title)
post.save(update_fields=['slug'])
def reverse_func(apps, schema_editor):
pass # just pass
class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.RunPython(generate_slugs_for_old_posts, reverse=reverse_func),
]
First of all you must add nullable slug field to your Post model. Here is Django docs on slug field. You must implement way of generating slug values too. You can implement generate_slug(...) method on your model for example:
import string # for string constants
import random # for generating random strings
# other imports ...
from django.utils.text import slugify
# other imports ...
class Post(models.Model):
# ...
slug = models.SlugField(null=True, blank=True, unique=True)
# ...
def save(self, *args, **kwargs):
self.slug = self.generate_slug()
return super().save(*args, **kwargs)
def generate_slug(self, save_to_obj=False, add_random_suffix=True):
"""
Generates and returns slug for this obj.
If `save_to_obj` is True, then saves to current obj.
Warning: setting `save_to_obj` to True
when called from `.save()` method
can lead to recursion error!
`add_random_suffix ` is to make sure that slug field has unique value.
"""
# We rely on django's slugify function here. But if
# it is not sufficient for you needs, you can implement
# you own way of generating slugs.
generated_slug = slugify(self.title)
# Generate random suffix here.
random_suffix = ""
if add_random_suffix:
random_suffix = ''.join([
random.choice(string.ascii_letters + string.digits)
for i in range(5)
])
generated_slug += '-%s' % random_suffix
if save_to_obj:
self.slug = generated_slug
self.save(update_fields=['slug'])
return generated_slug
Now on every object save you will automatically generate slug for your object.
To deal with old posts that do not have slug field set. You must create custom migration using RunPython (Django docs):
First run this command
python manage.py makemigrations <APP_NAME> --empty
Replace <APP_NAME> with your actual app name where Post model is located.
It will generate an empty migration file:
from django.utils.text import slugify
from django.db import migrations
def generate_slugs_for_old_posts(apps, schema_editor):
Post = apps.get_model("<APP_NAME>", "Post") # replace <APP_NAME> with actual app name
# dummy way
for post in Post.objects.all():
# Do not try to use `generate_slug` method
# here, you probably will get error saying
# that Post does not have method called `generate_slug`
# as it is not the actual class you have defined in your
# models.py!
post.slug = slugify(post.title)
post.save(update_fields=['slug'])
def reverse_func(apps, schema_editor):
pass # just pass
class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.RunPython(generate_slugs_for_old_posts, reverse=reverse_func),
]
After that point you may alter you slug field and make it non nullable:
class Post(models.Model):
# ...
slug = models.SlugField(unique=True)
# ...
Now python manage.py migrate, this will make slug fields non nullable for future posts, but it may give you a warning saying that you are trying to make existing column
non nullable. Here you have an option where it says "I have created custom migration" or something like that. Select it.
Now when your posts have slugs, you must fix your views so they accept slug parameters from url. The trick here is to make sure that your posts are also acceptable by ID. As someone already may have a link to some post with ID. If you remove URLs that takes an ID argument, then that someone might not be able to use that old link anymore.
you may also redirect old urls to the new ones
urls.py
[..]
from django.views.generic.base import RedirectView
urlpatterns = [
# Redirect old links:
path('article/<int:pk>', RedirectView.as_view(url='article/<slug:url>', permanent=True)),
# You won't need this path any more
# path('article/<int:pk>', ArticleDetailView.as_view(), name="article-detail"),
# The new path with slug
path('article/<slug:url>', ArticleDetailView.as_view(), name="article-detail"),
[..]
]
refer to https://docs.djangoproject.com/en/3.1/ref/class-based-views/base/#redirectview
Related
I'm building an e-commerce (like) project. While running the program, from Homepage, if we select a product I got hit by this error, instead of showing that product details. Now I can't figure out how to fix this please help me to fix it.enter image description here
Here's my Models.py
class Category(models.Model):
name=models.CharField(max_length=250,unique=True)
slug=models.SlugField(max_length=250,unique=True)
desc=models.TextField(blank=True)
image=models.ImageField(upload_to='category',blank=True)
class Meta:
ordering=('name',)
verbose_name='category'
verbose_name_plural='categories'
# for menu linking (url)
def get_url(self):
return reverse("ecommerce_app:products_by_category",args=[self.slug])
def __str__(self):
return '{}'.format(self.name)
class Product(models.Model):
name=models.CharField(max_length=250,unique=True)
slug = models.SlugField(max_length=250, unique=True)
desc = models.TextField(blank=True)
image = models.ImageField(upload_to='product', blank=True)
category=models.ForeignKey(Category,on_delete=models.CASCADE)
price=models.DecimalField(max_digits=10,decimal_places=2)
stock=models.IntegerField()
available=models.BooleanField(default=True)
created=models.DateTimeField(auto_now=True)
updated=models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
# for url
def get_url(self):
return reverse("ecommerce_app:ProdCatDetails",args=[self.category.slug,self.slug])
def __str__(self):
return '{}'.format(self.name)
Her's my Urls.py
from django.urls import path
from . import views
app_name='ecommerce_app'
urlpatterns=[
path('',views.allProdCat,name='allProdCat'),
# for special urls for Category
path('<slug:c_slug>/',views.allProdCat,name='products_by_category'),
# for special urls for Product Details
path('<slug:c_slug>/<slug:product_slug>/',views.ProdCatDetails,name='ProdCatDetails'),
]
Here's my Views.py
from django.http import HttpResponse
from .models import Category,Product
from django.shortcuts import render,get_object_or_404
# Create your views here.
def home(request):
return HttpResponse("Hello")
def allProdCat(request,c_slug=None):
c_page=None
products=None
if c_slug!=None:
c_page=get_object_or_404(Category,slug=c_slug)
products=Product.objects.all().filter(category=c_page,available=True)
else:
products=Product.objects.all().filter(available=True)
return render(request,'category.html',{'category':c_page,'products':products})
# For product details
def ProdCatDetails(request,c_slug,product_slug):
try:
product=Product.objects.get(category__product=c_slug,slug=product_slug)
except Exception as e:
raise e
return render(request,'product.htmal',{'product':product})
If you filter with:
product = Product.objects.get(category=c_slug, slug=product_slug)
You will filter on the field the product ForeignKey is referring to. Since that is not specified, this is the primary key, and thus an AutoField.
You can filter with:
# filter on the slug ↓
product = Product.objects.get(category__slug=c_slug, slug=product_slug)
Error with a slug in Django - template it shows all posts data in each post
when I create a new post and write my data it shows all data from other posts why is that?
and how I can fix it?
also how I can add an auto-generation slug?
models.py :
from django.urls import reverse
from django.utils.text import slugify
class Android(models.Model):
title = models.CharField(max_length=50,default="",help_text="this is title for slug not post!")
name = models.CharField(max_length=50,default="")
app_contect = models.CharField(max_length=240,default="")
app_image = models.ImageField(upload_to='images/',null=True, blank=True)
post_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
post_tag = models.CharField(max_length=50,default="",choices = BLOG_SECTION_CHOICES)
slug = models.SlugField(null=True,uniqe=True) # new
def get_absolute_url(self):
return reverse('android_posts', kwargs={'slug': self.slug}) # new
def get_image(self):
if self.app_image and hasattr(self.app_image, 'url'):
return self.app_image.url
else:
return '/path/to/default/image'
def __str__(self):
return self.name
class Meta:
ordering = ('-post_date',)
views.py :
def android_posts(request,slug):
android_posts = Android.objects.all()
context = {'android_posts':android_posts}
return render(request,'android/android_problems_fix.html', { 'android_posts': android_posts })
html page :
{% for android in android_posts %}
<h1 id="font_control_for_header_in_all_pages">{{android.name}}</h1>
<hr>
<p id="font_control_for_all_pages">{{android.app_contect}}</p>
{% endfor %}
url :
path('Android/<slug:slug>', views.android_posts, name='android_posts'),
To autogenerate your slug (and only do it on initial save, so that it remains consistent), add the generation to your model save method:
def save(self, *args, **kwargs):
super(<Model>, self).save(*args, **kwargs)
if not self.pk:
self.slug = <slugify code here>
As for your view/Template, you are specifically selecting all posts using:
android_posts = Android.objects.all()
Passing them to the template, then looping over them with the for loop to display them all.
Instead of this, select only a single object with:
android_post = Android.object.get(pk=<pk value>)
Edit after you added your urls.py:
You can get the unique object for a slug with:
android_post = get_object_or_404(Android, slug=slug)
The use of get_object_or_404 will also handle the case where that record doesn't exist.
You haven't posted your urls.py, so not sure how you're linking to this view, but if it includes the slug in the url, you will be able to get this in the view. My guess is you're not accessing via slug in the url, but via the id field.
Personally, when I slugify some text, I always include the id - it is a better way of ensuring uniqueness. By specifying unique=True on your non-pk slug field, you are likely restricting the titles people can use (2 people couldn't use the same title then!)
To give you an example, this is how I am doing it on one of my models:
def save(self, *args, **kwargs):
if not self.id or not self.slug:
super(Android, self).save(*args, **kwargs)
self.slug = slugify(f"{self.title} {str(self.id)}")
super(Android, self).save(*args, **kwargs)
This slug will always be unique because it includes id - and 2 people could have a record with the same title value without the system objecting.
I am trying to modify django-taggit to allow the same tag to be added by separate teams.
I have modified django-taggit Model so that it has user and team_id values added when a user adds a new tag to taggig_tag table. This also adds user and team_id values to taggit_taggeditems table.
The goal is to allow teams to edit or delete their own tags and that should not affect other teams, so different teams need to have their own separate sets of tags.
In my modified scenario, the tag name and team_id constitute a distinct tag. I expect i can test team_id or concatenate it to the tag name before django-taggit tests for distinct tags. But do not see where django-taggit does that.
Question: Where in the django-taggit code does it look for duplicate tag values?
`apps.py`
`forms.py`
`managers.py`
`models.py`
`utils.py`
`views.py`
My modified django-taggit Model code is below.
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import IntegrityError, models, router, transaction
from django.utils.text import slugify
from django.utils.translation import gettext, gettext_lazy as _
from django.conf import settings
### MODIFICATION: added django CRUM to get request user
from crum import get_current_user
try:
from unidecode import unidecode
except ImportError:
def unidecode(tag):
return tag
class TagBase(models.Model):
### MODIFICATION: added team and user to model, removed unique=True
name = models.CharField(verbose_name=_("Name"), max_length=100)
slug = models.SlugField(verbose_name=_("Slug"), max_length=100)
team_id = models.CharField(max_length=10, blank=False, null=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
def __gt__(self, other):
return self.name.lower() > other.name.lower()
def __lt__(self, other):
return self.name.lower() < other.name.lower()
class Meta:
abstract = True
def save(self, *args, **kwargs):
### MODIFICATION: added team and user to taggit_taggeditem model
### get request user with django CRUM get_current_user()
self.user = get_current_user()
self.team_id = get_current_user().team_id
if self._state.adding and not self.slug:
self.slug = self.slugify(self.name)
using = kwargs.get("using") or router.db_for_write(
type(self), instance=self
)
# Make sure we write to the same db for all attempted writes,
# with a multi-master setup, theoretically we could try to
# write and rollback on different DBs
kwargs["using"] = using
# Be oportunistic and try to save the tag, this should work for
# most cases ;)
### MODIFICATION: remove IntegrityError try/except for unique
which is removed
#try:
with transaction.atomic(using=using):
res = super().save(*args, **kwargs)
return res
#except IntegrityError:
# pass
### MODIFICATION: remove slugs create as no longer checking for
duplicate slugs
# Now try to find existing slugs with similar names
#slugs = set(
# self.__class__._default_manager.filter(
# slug__startswith=self.slug
# ).values_list("slug", flat=True)
#)
i = 1
#while True:
# slug = self.slugify(self.name, i)
# if slug not in slugs:
# self.slug = slug
# # We purposely ignore concurrecny issues here for now.
# # (That is, till we found a nice solution...)
# return super().save(*args, **kwargs)
# i += 1
while True:
slug = self.slugify(self.name, i)
#if slug not in slugs:
self.slug = slug
# We purposely ignore concurrecny issues here for now.
# (That is, till we found a nice solution...)
return super().save(*args, **kwargs)
i += 1
else:
return super().save(*args, **kwargs)
def slugify(self, tag, i=None):
slug = slugify(unidecode(tag))
if i is not None:
slug += "_%d" % i
return slug
class Tag(TagBase):
class Meta:
verbose_name = _("Tag")
verbose_name_plural = _("Tags")
app_label = "taggit"
class ItemBase(models.Model):
def __str__(self):
return gettext("%(object)s tagged with %(tag)s") % {
"object": self.content_object,
"tag": self.tag,
}
class Meta:
abstract = True
#classmethod
def tag_model(cls):
field = cls._meta.get_field("tag")
return field.remote_field.model
#classmethod
def tag_relname(cls):
field = cls._meta.get_field("tag")
return field.remote_field.related_name
#classmethod
def lookup_kwargs(cls, instance):
return {"content_object": instance}
class TaggedItemBase(ItemBase):
tag = models.ForeignKey(
Tag, related_name="%(app_label)s_%(class)s_items",
on_delete=models.CASCADE
)
class Meta:
abstract = True
#classmethod
def tags_for(cls, model, instance=None, **extra_filters):
kwargs = extra_filters or {}
if instance is not None:
kwargs.update({"%s__content_object" % cls.tag_relname():
instance})
return cls.tag_model().objects.filter(**kwargs)
kwargs.update({"%s__content_object__isnull" % cls.tag_relname():
False})
return cls.tag_model().objects.filter(**kwargs).distinct()
class CommonGenericTaggedItemBase(ItemBase):
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
verbose_name=_("Content type"),
related_name="%(app_label)s_%(class)s_tagged_items",
)
content_object = GenericForeignKey()
### MODIFICATION: added team and user to taggit_taggeditem model
team_id = models.CharField(max_length=10, blank=False, null=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
on_delete=models.DO_NOTHING)
class Meta:
abstract = True
#classmethod
def lookup_kwargs(cls, instance):
return {
"object_id": instance.pk,
"content_type": ContentType.objects.get_for_model(instance),
### MODIFICATION: added team and user to taggit_taggeditem model
"user": get_current_user(),
"team_id": get_current_user().team_id,
}
#classmethod
def tags_for(cls, model, instance=None, **extra_filters):
tag_relname = cls.tag_relname()
kwargs = {
"%s__content_type__app_label" % tag_relname:
model._meta.app_label,
"%s__content_type__model" % tag_relname: model._meta.model_name,
}
if instance is not None:
kwargs["%s__object_id" % tag_relname] = instance.pk
if extra_filters:
kwargs.update(extra_filters)
return cls.tag_model().objects.filter(**kwargs).distinct()
class GenericTaggedItemBase(CommonGenericTaggedItemBase):
object_id = models.IntegerField(verbose_name=_("Object id"),
db_index=True)
class Meta:
abstract = True
class GenericUUIDTaggedItemBase(CommonGenericTaggedItemBase):
object_id = models.UUIDField(verbose_name=_("Object id"), db_index=True)
class Meta:
abstract = True
class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
class Meta:
verbose_name = _("Tagged Item")
verbose_name_plural = _("Tagged Items")
app_label = "taggit"
### MODIFICATION: added team_id and user to taggit_taggeditems table
constraints
index_together = [["content_type", "object_id", "team_id", "user"]]
unique_together = [["content_type", "object_id", "tag", "team_id",
"user"]]
I have an app that will one day allow front-end crud, which will create the slug with slugify. Right now though, all the object creation is being done in the admin area and I was wondering if there is a way to auto generate slugs while creating and saving an object from within admin?
Here is the method for slugify for the front-end; not sure if its even relevant. Thank you.
def create_slug(instance, new_slug=None):
slug = slugify(instance.title)
if new_slug is not None:
slug = new_slug
qs = Veteran.objects.filter(slug=slug).order_by('-id')
exists = qs.exists()
if exists:
new_slug = '%s-%s' % (slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
Having just used this on another answer, I have exactly the right code in my clipboard. I do exactly this for one of my models:
from django.utils.text import slugify
class Event(models.Model):
date = models.DateField()
location_title = models.TextField()
location_code = models.TextField(blank=True, null=True)
picture_url = models.URLField(blank=True, null=True, max_length=250)
event_url = models.SlugField(unique=True, max_length=250)
def __str__(self):
return self.event_url + " " + str(self.date)
def save(self, *args, **kwargs):
self.event_url = slugify(self.location_title+str(self.date))
super(Event, self).save(*args, **kwargs)
Above solutions break validation in the Django Admin interface. I suggest:
from django import forms
from django.http.request import QueryDict
from django.utils.text import slugify
from .models import Article
class ArticleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ArticleForm, self).__init__(*args, **kwargs)
# Ensure that data is a regular Python dictionary so we can
# modify it later.
if isinstance(self.data, QueryDict):
self.data = self.data.copy()
# We assume here that the slug is only generated once, when
# saving the object. Since they are used in URLs they should
# not change when valid.
if not self.instance.pk and self.data.get('title'):
self.data['slug'] = slugify(self.data['title'])
class Meta:
model = Article
exclude = []
I have a model where the location of pdf directory I'm pointing to with my FilePathField is based on the "client" and "job_number" fields.
class CCEntry(models.Model):
client = models.CharField(default="C_Comm", max_length=64)
job_number = models.CharField(max_length=30, unique=False, blank=False, null=False)
filename = models.CharField(max_length=64, unique=False, blank=True, null=True)
pdf = models.FilePathField(path="site_media/jobs/%s %s", match=".*\.pdf$", recursive=True
#property
def pdf(self):
return "site_media/jobs/%s %s" % (self.client, self.job_number)
def __unicode__ (self):
return u'%s %s' % (self.client, self.filename)
class Admin:
pass
I've tried to pass the client and job_number data to the pdf field dynamically by using a #property method on the model class, but either my approach or my syntax is fualty because the entire pdf field disappears in the admin. Any pointers on what I'm doing wrong?
Based on your subsequent post on similar functionality in the FileField (see last link below)
And my inability to get any of the above to work, I'm gonna hazard a guess that it's not yet possible for the FilePathField field type.
I know that passing a callable works for most fields' 'default' parameters...
https://docs.djangoproject.com/en/dev/ref/models/fields/#default
... as it appears to work for the upload_to param of FieldField
(eg https://stackoverflow.com/questions/10643889/dynamic-upload-field-arguments/ ) andImageField` (eg Django - passing extra arguments into upload_to callable function )
Anyone interested in extending FilePathField to include this feature?
Anyone interested in extending FilePathField to include this feature?
I'd love to see this extension!
Just for the record, this is the solution that worked for me (django 1.3):
# models.py
class Analysis(models.Model):
run = models.ForeignKey(SampleRun)
# Directory name depends on the foreign key
# (directory was created outside Django and gets filled by a script)
bam_file = models.FilePathField(max_length=500, blank=True, null=True)
# admin.py
class CustomAnalysisModelForm(forms.ModelForm):
class Meta:
model = Analysis
def __init__(self, *args, **kwargs):
super(CustomAnalysisModelForm, self).__init__(*args, **kwargs)
# This is an update
if self.instance.id:
# set dynamic path
mypath = settings.DATA_PATH + self.instance.run.sample.name
self.fields['bam_file'] = forms.FilePathField(path=mypath, match=".*bam$", recursive=True)
class AnalysisAdmin(admin.ModelAdmin):
form = CustomAnalysisModelForm
Hope this helps somebody out there.
try to set the path value as callable function
def get_path(instance, filename):
return "site_media/jobs/%s_%s/%s" % (instance.client, instance.job_number, filename)
class CCEntry(models.Model):
....
pdf = models.FilePathField(path=get_path, match=".*\.pdf$", recursive=True)
but I'm not sure if this works, I didn't test it.
Added an implementation of this based on Django v1.9 FilePathField implementation:
from django.db.models import FilePathField
class DynamicFilePathField(FilePathField):
def __init__(self, verbose_name=None, name=None, path='', match=None,
recursive=False, allow_files=True, allow_folders=False, **kwargs):
self.path, self.match, self.recursive = path, match, recursive
if callable(self.path):
self.pathfunc, self.path = self.path, self.path()
self.allow_files, self.allow_folders = allow_files, allow_folders
kwargs['max_length'] = kwargs.get('max_length', 100)
super(FilePathField, self).__init__(verbose_name, name, **kwargs)
def deconstruct(self):
name, path, args, kwargs = super(FilePathField, self).deconstruct()
if hasattr(self, "pathfunc"):
kwargs['path'] = self.pathfunc
return name, path, args, kwargs
And example use:
import os
from django.db import models
def get_data_path():
return os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))
class GenomeAssembly(models.Model):
name = models.CharField(
unique=True,
max_length=32)
chromosome_size_file = DynamicFilePathField(
unique=True,
max_length=128,
path=get_data_path,
recursive=False)