When you dump your data fixtures, make sure you use the --natural argument:
python manage.py dumpdata myapp--indent = 4--natural
# Overrides deserialization to affect OneToOneFields for Users correctly import django.core.serializers.base from django.contrib.auth.models import User from your.attached.models import Attached #model with O2O field to User _original_save = django.core.serializers.base.DeserializedObject.save def save(self, * args, ** kwargs): if isinstance(self.object, Attached): # if the user in question has an attached object, delete it user = User.objects.get(pk = self.object.user_id) if hasattr(user, 'attached'): user.attached.delete() # use the built - in function for all other cases _original_save(self, * args, ** kwargs) django.core.serializers.base.DeserializedObject.save = save
Models created with one-to-one relation as documented fail to produce loadable fixtures. , Fixtures created with dumpdata and then reloaded with loaddata fail with this error: ,Model saving was too aggressive about creating new parent class instances during deserialization. Raw save on a model now skips saving of the parent class. , The patch works for every case with the exception of when the parent model and the child model share the same pk field name.
Fixtures created with dumpdata and then reloaded with loaddata fail with this error:
ERROR: duplicate key violates unique constraint "App_model_pkey"
Models:
from django.db
import models
# Create your models here.
class Parent(models.Model):
name = models.CharField(max_length = 60, unique = True)
class Child(Parent):
parent = models.OneToOneField(Parent, parent_link = True)
number = models.IntegerField(max_length = 20)
Dumpdata
$. / manage.py dumpdata--format = json first > first / fixtures / test.json
$ cat first / fixtures / test.json[{
"pk": 1,
"model": "first.parent",
"fields": {
"name": "first1"
}
}, {
"pk": 1,
"model": "first.child",
"fields": {
"name": "first1",
"number": 2
}
}]
$. / manage.py loaddata test
Installing json fixture 'test'
from '/home/zhaoz/programming/python/django_test/onetoone/../onetoone/first/fixtures'.
Problem installing fixture '/home/zhaoz/programming/python/django_test/onetoone/../onetoone/first/fixtures/test.json': column name is not unique
The implicit OneToOneFields created by concrete model inheritance don’t need explicit o2o() usage. Here’s an example with an additional model that inherits from bandaid.Band:,M2M relations, of course, can be defined from either end of the relation. This means that the following is also legal, and equivalent to the earlier example of creating the roadie-band relation inline:,If you’re not familiar with Django’s existing fixture system, please look at the relevant Django documentation before going further. Things like why fixtures exist, how and when they are used and what they are good for are not covered here. Coming here, you’ve optimally used Django’s fixtures for some time and, if you’re like me, gotten a little frustrated with them.,Nothing too special happens here; it relies on Django’s model inheritance functionality, where creating a MetalBand object will automatically create a Band object with the same primary key. You just need to be careful to not overlap the primary keys of any of the previously defined Band objects.
class Band(models.Model):
name = models.CharField(max_length = 255)
genre = models.CharField(max_length = 100)
band = Band.objects.create(name = "Bar Fighters", genre = "Rock")
[{
"pk": 1,
"model": "bandaid.band",
"fields": {
"name": "Bar Fighters",
"genre": "Rock"
}
}]
bandaid / __init__.py models.py fixtures / example_band.json
from class_fixtures.models
import Fixture
from bandaid.models
import Band
bands = Fixture(Band)
bands.add(2, name = "Brutallica", genre = "Metal")
bandaid / __init__.py models.py fixtures / __init__.py example_band.json more_bands.py
IntegrityError when loading Django fixtures with OneToOneField using SQLite,IntegrityError when using .save() on OneToOneField in Django,Django Models: Accessing Parent Object Attribute Within a Related Object Using OneToOneField,Django model u'id' clashes when using OneToOneField
If I understand your code organization correctly, the problem is in your imports. When you say
from.import Role
you're importing Role.py
into a module object named Role
. Inside that module (I assume) is your Role
model class. So what you really want is:
from.Role
import Role
Or:
from.import Role
...
role_id = models.OneToOneField(Role.Role)
You should define an app_label
in your meta class
class Foo(models.Model):
class Meta:
app_label = 'mainws'