Error creating and saving a Django-object from the other script

68 views
Skip to first unread message

tezro

unread,
Dec 9, 2009, 12:58:42 PM12/9/09
to Django users
Hi everyone. I'm using Django 1.1.0, importing it and using in another
file "robot_parser.py". I've been using it for months.

The script aggregates news from different sources and saves them using
Django ORM. Presently I used primary key for urls (http://www.site.com/
news/2009/jan/12/72828/), but then decided to use "slugs" (http://
www.site.com/news/2009/jan/12/slug-of-the-news/).

I changed the model adding slug field.
---
class Element(models.Model):
date = models.DateTimeField()
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200)
---

Also I added a field to MySQL table.

Here's the code that adds an element from the "robot_parser.py".
---
title = "test title"
slug = slugify(title)
new_element = Element(title=title, slug=slugify(title),
date=datetime.now())
new_element.save()
---

That throws an error: TypeError: 'slug' is an invalid keyword argument
for this function.

What am I doing wrong?

bruno desthuilliers

unread,
Dec 9, 2009, 4:21:00 PM12/9/09
to Django users


On 9 déc, 18:58, tezro <tezro...@gmail.com> wrote:
(snip)
>
> That throws an error: TypeError: 'slug' is an invalid keyword argument
> for this function.
>
> What am I doing wrong?

Not posting the full traceback - tracebacks are here to help debugging
a problem, not to fill your term with random gibberish !-)

tezro

unread,
Dec 9, 2009, 4:33:48 PM12/9/09
to Django users
That was really smart of you, thanks... Here's the traceback, hope it
helps.

---
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "robot_parser.py", line 383, in update
news = Element(source = the_source, title = title, date =
date_published, short = short, full = full, link = link, slug =
the_slug)
news.save()
File "/lib/python2.5/django/db/models/base.py", line 323, in
__init__
raise TypeError, "'%s' is an invalid keyword argument for this
function" % kwargs.keys()[0]
TypeError: 'slug' is an invalid keyword argument for this function
---


On Dec 10, 12:21 am, bruno desthuilliers

tezro

unread,
Dec 11, 2009, 4:51:20 PM12/11/09
to Django users
Seems like no clues...

On Dec 10, 12:21 am, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:

Shawn Milochik

unread,
Dec 11, 2009, 4:58:14 PM12/11/09
to django...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>


Tezro,

What Bruno meant is that, unless you post *all* of the text that printed out around the error message (not just that one line), you have not provided enough information for us to help you.

Anyone on this list can probably tell you exactly what that TypeError means. But telling you that won't solve your problem.

This error:
>>> TypeError: 'slug' is an invalid keyword argument

means that somewhere in your Python code, the keyword argument slug = "some value" is being passed to a function. That function doesn't know what to do with a 'slug' keyword, because that function's definition doesn't accept it, nor does it have a **kwargs portion.

So, although I've told you what the problem is, I haven't helped you much with your question "what am I doing wrong." But if you provide more of the information about the error, and preferably the section of code that is generating it, you will get help that is much more useful to solving the problem.

Shawn

tezro

unread,
Dec 11, 2009, 5:14:46 PM12/11/09
to Django users
Wooof, thanks for reply. Well, look. I had a "news/models.py" file
with definitions of Source (type of news) and Element(the news
elements theirselves) with a foreign key "Source".

The script I used to gather news from different sites looks like this:
---
title = "title"
date=datetime.now())
new_element = Element(title=title, date=date, source=source_object)
new_element.save()
---

Element's "get_absolute_url" returned "/news/2009/jan/12/7788/"
according to PK, wich is 7788 here. Then, I decided to make slugs for
news elements so that it returned some "useful" urls.

I added "slug = models.SlugField(max_length=200)" to the model and
added char field "slug" to mysql database. Then, changed the script
adding a slug field either to manual or "slugifying it" from title -
nothing worked:
---
title = "title"
date=datetime.now())
slug = "test-slug" #or slugify(title)
new_element = Element(title=title, date=date, source=source_object,
slug=slug)
new_element.save()
---

It keeps falling with that TypeError above. And... it throws
"_mysql_exceptions.Warning: Field 'slug' doesn't have a default value"
- fixing that from model or base doesn't help either.

But... the code works from the shell fine.

That's all.

On Dec 12, 12:58 am, Shawn Milochik <sh...@milochik.com> wrote:
> On Dec 11, 2009, at 4:51 PM, tezro wrote:
>
>
>
> > Seems like no clues...
>
> > On Dec 10, 12:21 am, bruno desthuilliers
> > <bruno.desthuilli...@gmail.com> wrote:
> >> On 9 déc, 18:58, tezro <tezro...@gmail.com> wrote:
> >> (snip)
>
> >>> That throws an error: TypeError: 'slug' is an invalid keyword argument
> >>> for this function.
>
> >>> What am I doing wrong?
>
> >> Not posting the full traceback - tracebacks are here to help debugging
> >> a problem, not to fill your term with random gibberish !-)
>
> > --
>
> > You received this message because you are subscribed to the Google Groups "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to django-users...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Shawn Milochik

unread,
Dec 11, 2009, 7:25:46 PM12/11/09
to django...@googlegroups.com

Why did you manually add a 'slug' field to your database after modifying the model? Django's syncdb does a lot more than just create the field; depending on which database you're using, it does a few other things and overrides some defaults. I wonder if there's a disconnect there. If this project is currently in production and you can't re-run syncdb, then I highly recommend South (south.aeracode.org) for database migrations.

I suspect that the problem is because the field was added manually. Try adding the field with South. It takes care of the nitty gritty details for you.

Worst-case, make a copy of the model in question with a different name. Then try to create new instances of it from your external script. If that works (after adding that new model to the database with syncdb), then it's almost definitely due to the way the model and database were modified after-the-fact manually.

Shawn






tezro

unread,
Dec 12, 2009, 7:51:52 AM12/12/09
to Django users
Added a migration with South... Ran it.

---
Running migrations for news:
- Migrating forwards to 0002_add_slug.
> news: 0002_add_slug
Traceback (most recent call last):
File "/home/tezro/lib/python2.5/South-0.6.2-py2.5.egg/south/
migration.py", line 330, in run_migrations
db.execute_deferred_sql()
File "/home/tezro/lib/python2.5/South-0.6.2-py2.5.egg/south/db/
generic.py", line 120, in execute_deferred_sql
self.execute(sql)
File "/home/tezro/lib/python2.5/South-0.6.2-py2.5.egg/south/db/
generic.py", line 86, in execute
cursor.execute(sql, params)
File "/home/tezro/apps/apache_django/lib/python2.5/django/db/
backends/mysql/base.py", line 84, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.5/site-packages/MySQLdb/cursors.py",
line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.5/site-packages/MySQLdb/
connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1061, "Duplicate key name 'news_element_slug'")
---

By the way, there was no slug field in the base. I made it the initial
way, just like I've never changed anything.

tezro

unread,
Dec 12, 2009, 7:56:59 AM12/12/09
to Django users
Oops, sorry. Didn't remove the index. Now it worked. Proceeding :)

tezro

unread,
Dec 12, 2009, 8:08:22 AM12/12/09
to Django users
Nope. Did it again on a clean project with the same models migrated
then by South.

Same error. Any other clues?

Thanks for replies.

Shawn Milochik

unread,
Dec 12, 2009, 9:56:56 AM12/12/09
to django...@googlegroups.com

On Dec 12, 2009, at 8:08 AM, tezro wrote:

> Nope. Did it again on a clean project with the same models migrated
> then by South.
>
> Same error. Any other clues?
>
> Thanks for replies.

When you say "same error," do you mean this one: "Duplicate key name 'news_element_slug'"?

If so, then it appears that, despite the clean project, it's not a clean database. Try using sqlite3 temporarily, or change the model name from News to NewsTest or something.

Shawn

tezro

unread,
Dec 12, 2009, 10:14:59 AM12/12/09
to Django users
Nope. Sorry for misdescription... Same error is "TypeError: 'slug' is
an invalid keyword argument" when using:
---
new_element = Element(title="title", date=datetime.now(),
source=source_object, slug="slug")
new_element.save()
---

And the same "_mysql_exceptions.Warning: Field 'slug' doesn't have a
default value" when using:
---
new_element = Element(title="title", date=datetime.now(),
source=source_object)
new_element.slug = "slug"
new_element.save()
---

The same errors from an outer script. From the shell or admin site -
works still fine...
Reply all
Reply to author
Forward
0 new messages