0

I have decided to implement registration option for my website, I used this tutorial (signup with confirmation part). Following this material I have created Profile module to hold some info. Everything (seems to be) working now, but the problem is that old profiles throws relatedObjectDoesNotExist error. According to these two questions (first, second) I need to make a migration to create profiles for old user accounts. I tried to follow this doc as suggested in one of the answers, but then I try to run a migration I get following error: KeyError: ('stv', 'bazinekaina')

stv is the name of my app and bazinekaina is the name of the next model after the one I need to create profiles.

How to limit migration to only the first model?

My relevant models.py code:

    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        email_confirmed = models.BooleanField(default=False)
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=30)
        email = models.EmailField(max_length=254)

    @receiver(post_save, sender=User)
    def update_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.get(user=instance)
        instance.profile.save()

#next model, this one throws an error, despite the fact it should not be touched at all
class BazineKaina(models.Model):
    #bazines kainos modelis

    bazka = models.DecimalField(max_digits=5, decimal_places=2)

    data = models.DateField(auto_now=False, auto_now_add=True)

    def __str__(self):
        return str(self.bazka)

    class Meta:
        verbose_name_plural = "Bazinė kaina"
        get_latest_by = 'data'

Migration file crated after using python manage.py makemigrations --empty stv command, named 0001_initial.py:

from django.db import migrations, models

def sukurti_profilius(apps, schema_editor):
    Profile = apps.get_model("stv", "Profile")
    for user in Profile.objects.all():
        Profile.objects.get_or_create(user=user)

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
    ]

How and what I should fix to stop migrations from applying to the unrelated models (and throwing error)?

Sorry if it is basic question, but whole django is still very new to me.

Gexas
  • 648
  • 4
  • 17
  • if your old profile are throwing error, delete all previous users and delete all the migration files and redo the makemigrations and migrations command – bmons Nov 12 '19 at 08:14
  • That is the thing, I would like to avoid deleting old users by creating profiles for them. – Gexas Nov 12 '19 at 08:17
  • 1
    try with get_or_create method in your model post save method – bmons Nov 12 '19 at 08:24

1 Answers1

1

If your migration is named 0001_initial then it means that you don't have a migration that actually creates the table for the profile model.

Remove that migration and run:

python manage.py makemigrations stv
python manage.py makemigrations stv --empty --name create_profiles

Then you should have a file 0002_create_profiles.py and put the logic to create profiles there.

marxin
  • 3,692
  • 3
  • 31
  • 44
  • 1
    It turns out I messed my migration files, re-downloaded them from Git and tried your advise. Combined with some other tweaks to some other parts of my code for this moment I got everything working, hope I will manage to move that out of development too. – Gexas Nov 12 '19 at 11:22