0

Since Django does not accept Foreign Keys as a slug, I currently do not have slugs setup on my model, however, I do have the slug field in the model. To get my slugs quickly, I created a page listing all the issues and used {{ issue|slugify }} and have them sorted by ID (which is the same ID in my PostgreSQL tables). In PostgreSQL, I can sort the issue tables by ID as well.

So my question is how do I insert the news slugs into the database? ** I am not familiar with PostgreSQL so baby talk would be nice.

4
  • I'm more than a little confused here, and I think you might be, too. A ForeignKeyField is simply an integer field in one table that contains the ID of a record in some other table. A SlugField is a CharField that has some extra constraints/capabilities. The two are unrelated. I'll also observe that Django sites are not for the technologically challenged. You can get away with point-and-click with WordPress, but not Django. Commented Jul 26, 2011 at 18:25
  • "I currently do not have slugs setup on my model, however, I do have the slug field in the model" This is pretty contradictory. So I'm not positive what you're asking Commented Jul 26, 2011 at 18:27
  • You're right. Sorry, there was a typo. What I meant is that I have the slug field, but there is no data in the field for each item. So, what I would like to do is update the slugfield with text without having to actually go through each item in the admin to do so. That would be timeworthy, as I'm sure there are other ways to do it. All I'm asking is to update the slugfield of each item. I am aware that a SlugField is a simple CharField. I've outgrown Wordpress, btw, and I've been able to fare fine with Django so far since I've been using it the past two months. Dear god. Commented Jul 27, 2011 at 1:16
  • And in the Django admin, you cannot prepopulate a ForeignKey for the slug - which is what I meant. Commented Jul 27, 2011 at 1:19

1 Answer 1

1

If you're just looking to update the value of your slug field for all your existing models you should be able to do it like this:

from django.db import F
from django.template.defaultfilters import slugify
YourModel.objects.all().update(your_slug_field=slugify(F('the_field_youre_slugging')))

I haven't tested this though.

However, it also doesn't address how you are going to set this slug field when you create your records. There are multiple ways to do this: override the save() method, a callable as default, as well as numerous others with various advantages and disadvantages.

Sign up to request clarification or add additional context in comments.

1 Comment

I did a test update - it makes the Slugfield in the Django admin say 'default.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.