Skip to content

Instantly share code, notes, and snippets.

@daddz
Created October 14, 2014 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daddz/0eaffdb22fb5838f6c91 to your computer and use it in GitHub Desktop.
Save daddz/0eaffdb22fb5838f6c91 to your computer and use it in GitHub Desktop.
class Foo(Model):
bars = ManyToManyField(Bar, related_name='foos', blank=True, null=True)
class Bar(Model):
name = CharField(unique=True)
# The process I know currently:
foo = Foo()
foo.save()
bar1 = Bar.objects.create(name='bar1')
bar2 = Bar.objects.create(name='bar2')
foo.bars.add(bar1)
foo.bars.add(bar2)
# What I would like to do
foo = Foo(bars=['bar1', 'bar2'])
foo.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment