Constraints when writing Python class - python

I`m using Python 3.5.2 and Django 1.11 using SQLite
I faced situation where i want to name my model Class with certain name, but with different names I get different results in manage.py migration regarding ForeignKeys
Example from shell:
modules.py
from django.db import models
class Place(models.Model):
question_text = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Place, on_delete=models.CASCADE)
Running python3 manage.py makemigrations post
Migrations for 'post':
post/migrations/0001_initial.py
- Create model Choice
- Create model Place
- Add field question to choice'
When renaming class Choice into in example "Rating":
from django.db import models
class Place(models.Model):
question_text = models.CharField(max_length=200)
class Rating(models.Model):
question = models.ForeignKey(Place, on_delete=models.CASCADE)
Output of migration attempt:
Migrations for 'post':
post/migrations/0001_initial.py
- Create model Place
- Create model Rating
You might notice that foreign key implementation disappeared. I`ve triple checked it everywhere. Tried with new projects. Stuck with this for a few days. Is there any class naming restrictions or some sort of a bug?
Have a nice holidays, thanks.

It's not a bug, in both cases the foreign key is created.
In the first case, the Choice model is created first, and since Place doesn't exist yet, it can't create the foreign key at the time the model is created. Therefore it is added later in a separate step after the Place model is created.
In the second case, the Place model is created first, so the foreign key can be created in the CreateModel operation for Rating.
Ideally, both cases would produce the second output whenever possible, as it contains less steps, but that optimisation hasn't been implemented yet.

Related

Create form for intermediate model showing all objects from one model

I’m working on a project to track Apps that are assigned to Employees. These assignments are stored in an intermediate model called EmployeeApps that contain an extra field called status (model.ChoiceField).
I need to build a form that displays a list of all Apps and a checkbox to request that app. This would save back to the EmployeeApps model any requested apps.
I have the various models created and working fine but cannot figure out how to create the form itself.
An identical question was asked a few years ago but the form logic wasn’t shared: Django with multiple ManyToManyField form
How can I get the list of all apps and display them alongside a checkbox that gets saved/updated back to EmployeeApps?
models.py
class Employees(models.Model):
name = models.CharField()
class Apps(models.Model):
name = models.CharField()
class EmployeeApps(models.Model):
employee = models.ForeignKey(“Employees”, on_delete=models.CASCADE)
apps = models.ForeignKey(“Apps”, on_delete=models.CASCADE)
status = models.IntegerField(choices=STATUS_CHOICES)

Django ManyToManyField Model Manager

In my Python Django 3.7 source code I have the following models:
class ExampleForeignKey(models.Model):
name = models.Charfield(max_length=200)
fields = models.ManyToManyField('ExampleField', related_name='example_foreign_keys')
class ExampleField(models.Model):
name = models.Charfield(max_length=200)
I experience an issue with database duplication when using threading with the source code:
example_field_obj = ExampleField(name='example_field_name')
example_foreign_key_obj = example_field_obj.example_foreign_keys.get_or_create(name='example_foreign_key_name')
I have managed to override the model manager get_or_create() method and applied a multithreading.Lock() in other cases where the model manager is being used directly in the case of direct 1:1 relationships however
I don't know whether it is possible to apply the same principle in the case of ManyToManyField relationships?
Apologies if this has been asked about before elsewhere - I can't seem to find much information on this particular issue.

Django 1.5 select_related defer with multi table inheritance

EDIT:
I fixed a few typos below
I added a zip file to a small app to demonstrate this problem here. You can download it and run python manage.py testselectrelateddefer after you syncdb and migrate.
I added a couple of observations below
I fix I am having a multi-table inheritance model as following:
class Image(models.Model):
# other fields are removed for simplicity
image = models.ImageField(upload_to="image")
class ItemImage(Image):
# other fields are removed for simplicity
display_name = models.CharField(max_length=50)
I want to query this model and defer the image field when I don't need the image (which is in the parent model). My container model looks somewhat like this:
class Item(models.Model):
item_image = models.OneToOneField(ItemImage)
The query looks like this:
test.models.Item.objects.select_related('item_image').defer("item_image__image").get(pk=1)
Django is throwing an error saying:
ValueError: u'image_ptr_id' is not in the list.
Django does not throw an error if I query for the field that is not in the parent model:
test.models.Item.objects.select_related('item_image').defer("item_image__display_name").get(pk=1)
Do you know how to fix this issue?
Observations:
As I mentioned above, this only happens if the deferred field is in the parent model; it does not happen if the deferred field is in the child model.
It does not matter if the parents field have any extra field.

python django one-to-one relations between two models

I have two models and want to set a relation to them.
class ModelA(models.Model):
id = models.IntegerField(primary_key=True) # DB => PK, AI, NN
name = models.CharField(max_length=50)
...
class ModelB(models.Model):
modelA = models.OneToOneField("ModelA", primary_key=True)
description = models.CharField(max_length=255)
...
So I have a relationship between the two models. Is it possible to add a member to ModelA which stores the relation to ModelB without saving this relation to the database?
I would call it a dynamically created relation or something. Any hints oder suggestions how to let both models know each other?
I think it would be benefiting if the relation on one model can be done dynamically. Otherwise I'll get some trouble storing the models because one of the IDs won't be stored if I save one of the models.
I want to have the relation on both models so I can easily use the models as inline in django-admin.
regards
The reverse relation in Django is created by default.
To get the ModelA you will use:
ModelA.objects.filter(modelb__pk = 1)
You will find more details here:
https://docs.djangoproject.com/en/dev/topics/db/queries/
Django ORM will save ModelA first, then ModelB, in order to maintain data integrity in the DB.
Django can try saving multiple items in one transaction, and this way, if you cancel it, nothing will be saved, but this is possible in shell or in Python code. Over HTTP you can't maintain a transaction over several queries so far.
If you need to show model A as inline of model B, you need a custom admin interface, not new fields/models. I can't tell how to write custom admin widgets. I did do a similar thing with custom editor views & templates & Javascript. I stored the unsaved models in request.session.

Creating field value "unique" throughout the collection in Django MongoDB models?

I am having hard time to understand how to make field value unique while using Django MongoDB.my models.py has a model:
class Projects(models.Model):
projectName =models.CharField(max_length = 100,unique=True)
projectManager = EmbeddedModelField('Users')
Here i want whenever a new project instance is added it should have unique projectName.But this code is not working out as it allows adding same value for projectName and doesn't give me error.I read its possible to make field value Unique by using indexes in pymongo but how do I do it in Django MongoDB.
Answer to my own question is I had to add unique=True for model field before doing syncdb.Thanks to culebron.Its working now

Categories

Resources