Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23296 closed Bug (fixed)

Invalid parameter to create() in RunPython example

Reported by: Areski Belaid Owned by: Areski Belaid
Component: Documentation Version: 1.7-rc-2
Severity: Normal Keywords: migrations
Cc: Areski Belaid, cmawebsite@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following RunPython documentation: https://docs.djangoproject.com/en/dev/ref/migration-operations/#runpython

I noticed that I could not get the example running if I use argument using=db_alias.

It raises the following error:

  File "../../django/django/db/models/base.py", line 457, in Model.__init__
    self = <ComplexModel: ComplexModel object>
    *args = ()
    **kwargs = {'using': 'default'}
    455             pass
    456     if kwargs:
--> 457         raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
                args_len = 0
                field = <django.db.models.fields.CharField: name>
                fields_iter = <listiterator object at 0x2ab5a50>
                is_related_object = False
                prop = 'using'
                val = u'name1'
    458 super(Model, self).__init__()
    459 signals.post_init.send(sender=self.__class__, instance=self)
TypeError: 'using' is an invalid keyword argument for this function

Nevertheless if I remove using=db_alias it works fine, replacing:

    db_alias = schema_editor.connection.alias
    Country.objects.create(name="USA", code="us", using=db_alias)
    Country.objects.create(name="France", code="fr", using=db_alias)

by:

    Country.objects.create(name="USA", code="us")
    Country.objects.create(name="France", code="fr")

Is this a bug or an error in the documentation?

Change History (6)

comment:1 by Collin Anderson, 10 years ago

Cc: cmawebsite@… added
Component: MigrationsDocumentation
Triage Stage: UnreviewedAccepted
Version: master1.7-rc-2

I think that's a problem with the docs. I don't think create() accepts using.

Maybe should we say?
Country(name="USA", code="us").save(using=db_alias)

This syntax may also work:
Country.objects.using(db_alias).create(name="USA", code="us")

comment:2 by Tim Graham, 10 years ago

Summary: RunPythonInvalid parameter to create() in RunPython example

If we are showing best practices, how about using .using(db_alias).bulk_create(...)?

comment:3 by Areski Belaid, 10 years ago

Owner: changed from nobody to Areski Belaid
Status: newassigned

+1 for best practices!

Here a PR if we are happy with this change

comment:4 by Collin Anderson, 10 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

lgtm

comment:5 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 7ca665c5f593953cba2cfeebd4f82acd38231031:

Fixed #23296 -- Fixed RunPython code sample in Migration Operations.

comment:6 by Tim Graham <timograham@…>, 10 years ago

In 28d97533246fa1051e416ff772a40190b1f30e4d:

[1.7.x] Fixed #23296 -- Fixed RunPython code sample in Migration Operations.

Backport of 7ca665c5f5 from master

Note: See TracTickets for help on using tickets.
Back to Top