Opened 8 months ago

Closed 8 months ago

Last modified 8 months ago

#23296 closed Bug (fixed)

Invalid parameter to create() in RunPython example

Reported by: areski Owned by: areski
Component: Documentation Version: 1.7-rc-2
Severity: Normal Keywords: migrations
Cc: areski, 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 Changed 8 months ago by collinanderson

  • Cc cmawebsite@… added
  • Component changed from Migrations to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from master to 1.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 Changed 8 months ago by timgraham

  • Summary changed from RunPython to Invalid parameter to create() in RunPython example

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

comment:3 Changed 8 months ago by areski

  • Owner changed from nobody to areski
  • Status changed from new to assigned

+1 for best practices!

Here a PR if we are happy with this change

comment:4 Changed 8 months ago by collinanderson

  • Has patch set
  • Triage Stage changed from Accepted to Ready for checkin

lgtm

comment:5 Changed 8 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 7ca665c5f593953cba2cfeebd4f82acd38231031:

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

comment:6 Changed 8 months ago by Tim Graham <timograham@…>

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