Ticket #8419: option2-create-patch.diff

File option2-create-patch.diff, 1.7 KB (added by Richard Davies <richard.davies@…>, 16 years ago)

Moving to create(), this makes the force_insert and force_update flags available - an alternative

  • django/db/models/query.py

     
    302302        raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
    303303                % (self.model._meta.object_name, num, kwargs))
    304304
    305     def create(self, **kwargs):
     305    def create(self, force_insert=False, force_update=False, **kwargs):
    306306        """
    307307        Creates a new object with the given kwargs, saving it to the database
    308308        and returning the created object.
     309
     310        The 'force_insert' and 'force_update' parameters can be used to insist
     311        that the "create" must be an SQL insert or update (or equivalent for
     312        non-SQL backends), respectively. Normally, they should not be set.
    309313        """
    310314        obj = self.model(**kwargs)
    311         obj.save()
     315        obj.save(force_insert=force_insert, force_update=force_update)
    312316        return obj
    313317
    314318    def get_or_create(self, **kwargs):
  • docs/ref/models/querysets.txt

     
    560560
    561561are equivalent.
    562562
     563Note that if you specify the primary key value of an existing object then
     564``create()`` acts as a one line shorthand for updating that object.
     565
     566Advanced users who wish to force the ``create()`` method to perform an SQL
     567``INSERT`` or an SQL ``UPDATE`` can pass the ``force_insert=True`` or
     568``force_update=True`` parameters to ``create()``, just like ``save()``
     569itself.
     570
    563571``get_or_create(**kwargs)``
    564572~~~~~~~~~~~~~~~~~~~~~~~~~~~
    565573
Back to Top