How can I set/provide a default value while django migration? - django

Scenario:
I have a model, Customer
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
and now I updated the company attribute witha ForeignKey relationship as below,
class Company(models.Model):
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.ForeignKey(Company)
What I need is, when the new migrations applied to the DB,corresponding Company instance must automatically generate and map to the company attribute of Customer instance.Is that possible? How can I achieve this ?

Let's start from your original model and do it step by step.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
First you would have to keep the original field and create a new one, to be able to restore the old data afterwards.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
_company = models.ForeignKey(Company)
Now you can create a first migration with manage.py makemigrations. Then you will have to create a data migration. Create the migration using manage.py makemigrations yourapp --empty and update the generated file:
from django.db import migrations
def export_customer_company(apps, schema_editor):
Customer = apps.get_model('yourapp', 'Customer')
Company = apps.get_model('yourapp', 'Company')
for customer in Customer.objects.all():
customer._company = Company.objects.get_or_create(name=customer.company)[0]
customer.save()
def revert_export_customer_company(apps, schema_editor):
Customer = apps.get_model('yourapp', 'Customer')
Company = apps.get_model('yourapp', 'Company')
for customer in Customer.objects.filter(_company__isnull=False):
customer.company = customer._company.name
customer.save()
class Migration(migrations.Migration):
dependencies = [
('yourapp', 'xxxx_previous_migration'), # Note this is auto-generated by django
]
operations = [
migrations.RunPython(export_customer_company, revert_export_customer_company),
]
The above migration will populate your Company model and Customer._company field according to Customer.company.
Now you can drop the old Customer.company and rename Customer._company.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.ForeignKey(Company)
Final manage.py makemigrations and manage.py migrate.

Sure, but you have to do three migrations and the fields cant be named the same thing as both need to exist at the same time. If you already have removed the company field in your real database you are SOL and will have to fix them manually.
First, add the Company model in a normal db migration, then do a data migration and have it run after the first db migration, then do another db migration removing the company field from the Customer model.
The db migrations you can do with manage.py makemigrations as usual, just add something like below in a migration file between them, here i named the new company ForeignKey field to company_obj
def fix_companies(apps, schema_editor):
Company = apps.get_model("myapp", "Company")
Customer = apps.get_model("myapp", "Customer")
for c in Customer.objects.all():
company, _ = Company.objects.get_or_create(name=c.name)
c.company_obj = company
c.save()
def rev(apps, schema_editor):
# the reverse goes here if you want to copy company names into customer again if you migrate backwards.
pass
class Migration(migrations.Migration):
dependencies = [
('myapp', 'XXXX_migration_that_added_company_model'),
]
operations = [
migrations.RunPython(fix_companies, rev),
]

Something to note is if you are going through a cycle of renaming/ deprecating fields, using RunPython would leave you pointing to old model fields that wouldn't exist anymore after you are done with your field changes.
To avoid this, you might want to go with RunSQL instead.
# Generated by Django 3.2.3 on 2022-02-09 04:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("<your_app>", "<0006_migration_name>"),
]
operations = [
migrations.RunSQL(f"""
update public.<table_name> set new_field = old_field + some_magic;
"""
),
]
Docs.

Related

Django inspectdb lost comment

I use mysql to test model.
With django_comment_migrate helps, help_text saved to sql table field comment.
Original model.py below:
from django.db import models
class Foo(models.Model):
name = models.CharField(max_length=20, verbose_name='alias_name', help_text='alias_name')
Run python manage.py inspectdb got result below, lost help_text.
class App1Foo(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=20)
class Meta:
managed = False
db_table = 'app1_foo'

Rename a field which was Primary Key in models.py

I have a model with the following fields:
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
department = models.TextField(verbose_name="department")
username = models.CharField(max_length=30, unique=True)
emp_id = models.AutoField(primary_key=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
hide_email = models.BooleanField(default=True)
name = models.TextField(verbose_name="employee name")
I want to rename "emp_id" field to "id", as you can see it is the primary key field. Can you please create a migration file for the same?
Thank you
You modify the field to:
class MyModel(models.Model):
# …,
id = models.AutoField(primary_key=True, db_column='emp_id')
# …
If you then run manage.py makemigrations, Django will ask if you renamed the fied:
Did you rename mymodel.emp_id to mymodel.id (a AutoField)? [y/N]
a question you answer with yes.
This will still use the emp_id as the database id. If you want to rename the database column as well, you can just remove the emp_id field, and Django will use the default id field as primary key instead, and you can let Django make migrations the same way.
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('your_app_name', 'name_of_previousmigration_file'),
]
operations = [
migrations.RemoveField(
model_name='modelname',
name='emp_id',
)
]
class ModelName(models.Model):
id = models.AutoField(primary_key=True)
# other fields
Then run python manage.py makemigrations
A message will appear which will tell you to confirm that you want to rename the field emp_id to id. Type y to confirm yes.
Then run python manage.py migrate
In you application directory you'll found migration files.

django select records from multiple tables with same foreign key

I would like to execute a single query in Django which retrieves related data, by foreign key, in multiple tables. At present I have to run a query on each table e.g. (House, Furniture, People) using the House number as a filter.
In SQL I can do this in one query like this:
SELECT house.number, house.number_of_rooms, furniture.type, people.name
FROM (house INNER JOIN furniture ON house.number = furniture.house_number)
INNER JOIN people ON house.number = people.house_number
WHERE (((house.number)="21"));
Can this be done in Django?
See example models below:
class House(models.Model):
number = models.CharField('House Number', max_length=10, blank=True, unique=True, primary_key=True)
number_of_rooms = models.IntegerField(default=1, null=True)
class Furniture(models.Model):
house_number = models.ForeignKey(House, on_delete=models.CASCADE, null=True)
type = models.CharField('Furniture Type', max_length=50)
class People(models.Model):
house_number = models.ForeignKey(House, on_delete=models.CASCADE, null=True)
first_name = models.CharField('First Name', max_length=50)
In your models add related_name arguments for foreign keys, so that you can retrieve the objects related to the House() instance.
class Furniture(models.Model):
house_number = models.ForeignKey(House, related_name='house_furniture', on_delete=models.CASCADE, null=True)
type = models.CharField('Furniture Type', max_length=50)
class People(models.Model):
house_number = models.ForeignKey(House, related_name='house_people', on_delete=models.CASCADE, null=True)
first_name = models.CharField('First Name', max_length=50)
Then run the migration using following commands.
python manage.py makemigrations
python manage.py migrate
Then create a new serializers.py module in the same app.
#import models Furniture, People, house
from rest_framework import serializers
class FurnitureSerializer(serializer.ModelSerializer):
class Meta:
model = Furniture
fields = ['type'] # if you want all the fields of model than user '__all__'.
class PeopleSerializer(serializer.ModelSerializer):
class Meta:
model = People
fields = ['first_name'] # if you want all the fields of model than user '__all__'.
class HouseSerializer(serializer.ModelSerializer):
house_furniture = FurnitureSerializer(many=True)
house_people = PeopleSerializer(many=True)
class Meta:
model = Furniture
fields = ['number', 'number_of_rooms', 'house_furniture', 'house_people']
Now, in your views.py you can simply query on model House and serializer the result with HouseSerializer().
#import models from models.py
#import serializer from serializers.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.generics import ListAPIView
class ListHouseView(ListAPIView):
serializer_class = HouseSerializer
queryset = House.objects.filter() #here you can apply filters on the fields of house model and user using related_name you can filter on other related models as well.
Now, simply call ad this in your app's urls.py
url_pattern = [
path('list-house/', ListHouseView.as_view()),
]
Make sure that have a path in your project's urls.py to reach this app's urls.py.
The usual Django way of dealing with this is Queryset.prefetch_related() and iterating through Python (unless you're using Postgres, which has its own solution of ArrayAgg). Given your models, it'll cost three queries, but you won't have to deal with de-normalized row results.
h = House.objects.prefetch_related('furniture_set', 'people_set').get(number='21')
for furniture in house.furniture_set.all():
print(furniture)
for person in house.people_set.all():
print(people)
prefetch_related() caches the results and does the "joining" in Python once the queryset is evaluated, so iterating through the reverse relationships won't incur additional queries, and you're free to structure/serialize the data however you like. The raw SQL from this is something like:
SELECT house.number, house.number_of_rooms FROM house WHERE house.number = '1'
SELECT furniture.id, furniture.house_number_id, furniture.type FROM furniture WHERE furniture.house_number_id IN ('1')
SELECT people.id, people.house_number_id, people.first_name FROM people WHERE people.house_number_id IN ('1')
But Django does that behind-the-scenes so that you can just deal with a model instance in Python.

How to save a table entry in django

I am working on a django admin based project now i am stuck with a big thing.i want to add a field named "item_issued" in the user_profile model.
in the "item issued" field there is a table which consist of 3 column "item_name","quantity" and "price".I am unable to apply this.Can u guys please help me in this?
Thanks in advance
If I understand you correctly you want to add a ForeignKey to your user_profile pointing to item_issued. You can accomplish that by creating a new model ItemIssued with the fields you mentioned:
class ItemIssued(models.Model):
item_name = models.CharField(max_length=100)
quantity = models.IntegerField()
price = models.FloatField()
Now, when you're having ItemIssued model you can add a ForeignKey to user_profile (I assume the model is called UserProfile):
class UserProfile(models.Model):
... # your existing fields
item_issued = models.ForeignKey(ItemIssued)
After that, don't forget to run
python manage.py makemigrations app
python manage.py migrate
Here is a starting point:
models.py:
class ItemIssued(models.Model):
item_name = models.CharField(max_length=50)
quantity = models.IntegerField()
models.DecimalField(max_digits=6, decimal_places=2) #use decimal field for price values.
class UserProfile(models.Model):
# some other fields..
issued_items = models.ManyToManyField("ItemIssued", related_name="+issued_items", null=True, blank=True)
And if you need to use this field outside of Django Admin, views.py:
user = UserProfile.objects.get(username="ali")
new_issued_item = ItemIssued.objects.get(item_name="test_item")
user.issued_items.add(new_issued_item) #add
user.issued_items.delete(new_issued_item) #delete
items = user.issued_items.all() # get all issued items of user
i didn't test the code. But they should work.

Migrating data with south to a new model not working

I have an abstract model that I'm converting to a concrete model. I'm successfully using south to change the schema, but I'm unable to use the datamigration.
My initial state is:
class UserProfile(models.Model):
user = models.OneToOneField(User, primary_key=True, \
related_name='profile')
class Meta:
abstract=True
class SpecificProfile(UserProfile):
url = models.URLField()
My new state is:
class UserProfile(models.Model):
user = models.OneToOneField(User, primary_key=True, \
related_name='profile')
class SpecificProfile(UserProfile):
user_profile = models.OneToOneField(UserProfile, parent_link=True)
url = models.URLField()
My schema migration is:
class Migration(SchemaMigration):
def forwards(self, orm):
# Renaming field 'SpecProfile.user_profile'
db.rename_column('specificprofile', 'user_id', 'user_profile_id')
# Adding model 'UserProfile'
db.create_table('userprofile', (
('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='profile', unique=True, primary_key=True, to=orm['auth.User'])),
))
db.send_create_signal('myapp', ['UserProfile'])
I edited the file generated by south in order to rename one field in SpecificProfile
Now, in the data migration process, I would like to create one UserProfile entry per SpecificProfile and assign UserProfile.user_id to SpecificProfile.user_profile_id.
So, my data migration forwards is:
class Migration(DataMigration):
def forwards(self, orm):
for spec in orm.SpecificProfile.objects.all():
user_profile = orm.UserProfile()
user_profile.user_id = spec.user_profile_id
user_profile.save()
The script runs without errors but does not create any new entry in UserProfile table.
Should I use UserProfile() instead of orm.UserProfile()?
Any ideas?
SpecificProfile.user_profile_id didn't exist previously, so it has no data to migrate. What you really are wanting to do is set user_profile.user to spec.user, and then set spec.user_profile to user_profile.
def forwards(self, orm):
for spec in orm.SpecificProfile.objects.all():
user_profile = orm.UserProfile()
user_profile.user_id = spec.user_id
user_profile.save()
# Then,
spec.user_profile_id = user_profile.id
However, once you've done the initial migration, I'm pretty sure SpecificProfile.user doesn't exist anymore. South removes that field since it's now on UserProfile.