Snippet List
This lets you load global fixtures from a directory you set as `FIXTURES_ROOT` in your settings.py.
For example, setting `FIXTURES_ROOT` to `/path/to/myproject/fixtures/`
I tend to like to keep fixtures that should be loaded when a new instance of a site is deployed, but not auto-loaded (so they won't rewrite any data that comes after), in a project-level fixtures directory like this.
Sometimes these fixtures can be useful in your test suites, so this is a convenient way to load them.
Usage:
`load_global_fixtures('sites.json', 'contacts.json')`
`load_global_fixtures('sites.json', 'contacts.json', fixtures_root='/some/other/path', verbosity=1)`
You can extend the class **ModifiedModel** to set new fields, replace existing or exclude any fields from a model class without patch or change the original code.
**my_app/models.py**
from django.db import models
class CustomerType(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Customer(models.Model):
name = models.CharField(max_length=50)
type = models.ForeignKey('CustomerType')
is_active = models.BooleanField(default=True, blank=True)
employer = models.CharField(max_length=100)
def __unicode__(self):
return self.name
**another_app/models.py**
from django.db import models
from django.contrib.auth.models import User
from this_snippet import ModifiedModel
class City(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class HelperCustomerType(ModifiedModel):
class Meta:
model = 'my_app.CustomerType'
description = models.TextField()
class HelperCustomer(ModifiedModel):
class Meta:
model = 'my_app.Customer'
exclude = ('employer',)
type = models.CharField(max_length=50) # Replaced
address = models.CharField(max_length=100)
city = models.ForeignKey(City)
def __unicode__(self):
return '%s - %s'%(self.pk, self.name)
class HelperUser(ModifiedModel):
class Meta:
model = User
website = models.URLField(blank=True, verify_exists=False)
- fields
- model
- helper
- change
Django provides a `get_or_create` helper method in the `models.Manager` class which looks up an object for the given `kwargs`, creating one if necessary. But sometime you need a method which updates the object with given `kwargs` or creates it if it's not found. This snippet provides the helper for that purpose.
Use the snippet like this:
from django.db import models
class PersonManager(models.Manager):
update_or_create = _update_or_create
class Person(models.Model):
first_name = models.CharField()
last_name = models.CharField()
city = models.CharField()
objects = PersonManager()
person, created, updated = Person.objects.update_or_create(first_name="John",
last_name="Smith", defaults=dict(city="London"))
The method returns a tuple of (`object`, `created`, `updated`), where `created` and `updated` are booleans specifying whether an `object` was created or updated respectively. Both `created` and `updated` are `false` if `object` is neither created nor updated (that is `object` has just been fetched "as is" from db). This happens if the update fails.
- model
- helper
- update
- create
6 snippets posted so far.