I'm using django MPTT model.
I subclass MPTT model, but then try to add fixture to the custom model with supplied initial_data in JSON.
The parent TreeForeignKey is optional (blank=True, null=True)
When I apply a JSON fixture from initial_data, it asks to supply the fields "lft", "rght", "tree_id", "level".
ie: may not be NULL
...when running python manage.py syncdb
These are fields from MPTT.
Is there a way to exclude this or get around this from the fixture data?
Thanks
Related
I'm writing a test in Django 1.11 and trying to load some fixture data into my local sqlite database (retrieved from the real database via manage.py dumpdata) like so:
class MyTest(TestCase):
fixtures = [os.path.join(settings.BASE_DIR, 'myapp', 'tests', 'fixtures.json')]
Part of the fixture data is User models where we have a custom User with a different USERNAME_FIELD, so all of the username fields on each of our records is blank.
However, when I try and run my test using my fixture, I get the following error:
IntegrityError: Problem installing fixture '.../fixtures.json': Could not load myapp.CustomUser(pk=41): column username is not unique
pk 41 is the second CustomUser record in my fixtures.json. How can I tell Django's testing framework to ignore the constraint it thinks it needs to enforce, or otherwise get my fixture data loaded for testing?
i had a profile field in todo models which a ForeignKey to Profile model
Instead of using profile i want to use the user field of User model as a ForeignKey .
Steps:
I have removed the profile field from todo model, created and applied migration for that.
Works fine, profile field do not exist in database.
2, I added user field as a ForeignKey in todos models .
When doing python manage.py schemamigration todos --auto
gives response like:
Nothing seems to have changed.
what am missing here ?
I've got a Django app I've created which uses both Django Guardian to manage object-level permissions, and Django South to manage migrations. I've created a model mixin which allows models to define object-level permissions to be assigned as they are created, using a custom Meta attribute. So for example a Message model might look like:
class Message(AutoUserPermissionsMixin, models.Model):
sender = models.ForeignKey(User)
recipient = models.ForeignKey(User)
text = models.TextField(blank=True)
class Meta:
permissions = (
('view_message', 'Can view message'),
('respond_to_message', 'Can respond to message'),
)
user_permissions_to_add = {
'recipient' : ('view_message', 'respond_to_message',),
'sender' : ('view_message',)
}
The AutoUserPermissionsMixin defines a custom save() which reads the model's Meta to know which object-level permissions should be assigned to which field from user_permissions_to_add, and does the assignment. I added the custom Meta field by doing this in the top of file where I define AutoUserPermissionsMixin:
from django.db import models
models.options.DEFAULT_NAMES += ('user_permissions_to_add',)
The problem is, I'm trying to do a data migration in South to create a number of new model instances, and it doesn't assign the object-level permissions, because custom save() methods aren't handled in a migration.
Now I can use the same method being used in the custom save() to try and apply the permissions in the model, which is sync_object_permissions(instance, permissions). I want to read in the permissions at whatever state they are in on the Meta at the time of migration, not hard-code them into the migration. However, when trying to call sync_object_permissions(message_instance, permissions=message_instance._meta.user_permissions_to_add), South throws the error:
AttributeError: 'Options' object has no attribute 'user_permissions_to_add'
So for some reason, the Meta isn't being update with my custom user_permissions_to_add attribute at the time of migration. How can I make sure it's there on the Meta in the migration?
South's modelinspector can be modified to slurp up your extra field on Meta so that this field is avilable during migrations.
Here's the code I've used (put this in your models.py file or some other file that it imports):
#make South record the Meta field "my_special_field" in its orm freezes
import south.modelsinspector as mi
mi.meta_details['my_special_field'] = ['my_special_field', {'default': None, 'ignore_missing': True}]
Note that if you do not include 'ignore_missing': True then South will throw an exception when it processes any models that do not include 'my_special_field' in their Meta options
You can inspect the other Meta fields that South already records by looking at the other fields on south.modelsinspector.meta_details
I have changed django.contrib.auth.user model where I have made the email id to be unique. After which I added a datamigration to reflect the same-
python manage.py datamigration appname unique_password.
Followed by python manage.py schemamigration appname --auto-->no changes. Finally, python manage.py migrate appname.
Migrating forwards to 0003_unique_password
appname:0037_unique_password.py
Now when I add a user whose email is not unique, it gives me an error and does not let me create the user through the django admin. But when i do:
>
python manage.py shell
from django.contrib.auth.models import User
user=User('cust1','123','xxx#gmail.com')
This creates a user object even though 'xxx#gmail.com' already exists.
How can I add an unique constraint to django.contrib.auth.user.email for an existing project (with data)?
Checks for uniqueness occur during model validation as per the Model validation documentation. Calling save() on the model doesn't automatically cause model validation. The admin application will be calling model validation.
Nothing prevents you from creating a User object in Python such as user=User('cust1','123','xxx#gmail.com') and then doing a user.save(). If there are uniqueness checks at the database level, you'll likely get an exception at this point. If you want to check before doing the save, call is_valid() on the Model instance.
If you want every User object to be automatically validated before save, use the pre_save Django signal to do Model validation before the save proceeds.
Here is model structure: Client is User, Client can be corporate or person:
class Client(User):
#fields
class ClientCorporate(Client):
#fields
class ClientPerson(Client):
#fields
And client can make orders:
class Order(models.Model):
client=models.ForeignKey(Client)
#fields
I try to create common orders list for any type of client, here is view:
def orders_list(request):
client=request.user.client
return list_detail.object_list(request,
queryset = client.order_set.all(),
#templates ...
)
And it leads to an error:
DatabaseError while rendering: no such column: cabinets_order.client_id
I browse db and find that all User childs have column "user_prt_id". So, what's the best way to make it work in django? Create custom manager, or change models in appropriate way? ...
This error just means you don't have the database column client_id in your cabinets_order table.
Make sure you python manage.py syncdb. If you added the foreign key after syncdbing once, you'll have to reset it, or add the foreign key manually. You can generate the SQL needed by running python manage.py sqlall cabinets, then picking the ForeignKey generation bits.
If you can stand to lose your current data (if in development), just run python manage.py reset cabinets