Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove poly base model #523

Open
joejoe-am opened this issue Jul 17, 2022 · 1 comment
Open

remove poly base model #523

joejoe-am opened this issue Jul 17, 2022 · 1 comment

Comments

@joejoe-am
Copy link

hey all wish have a good day. i have some models like this:

class BaseModel(PolymorphicModel):
    review_average = models.FloatField(default=0.0, verbose_name=_('Review Average'))
    tags = tagulous.models.TagField(to=Tag, blank=True)
    sort = models.IntegerField(default=0, verbose_name=_('Sort'))
class Teacher(BaseModel):
    user_obj = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('User'))
    slider_segment_obj = models.ForeignKey(SliderSegment, null=True, blank=True, on_delete=models.CASCADE, verbose_name=_('Slider Semgent'))
    biography = models.TextField(null=False, blank=False, verbose_name=_('Biography'))

now i want to delete poly base model and transfer records from base model to teachers table.
i cant create new class like NewTeacher because i used many times teacher model in project. any documentation to do this ?

@Asday
Copy link

Asday commented Nov 17, 2022

Add the fields review_average_temp, tags_temp, and sort_temp to Teacher as nullable fields and makemigrations. Make a data migration that copies all data from BaseModel to the temporary rows in Teacher. Remove the base model from Teacher instead inheriting from models.Model and makemigrations. Rename the fields you added to remove temp and makemigrations.

Make sure you check the generated migrations each time to see that it's picking up the right renames and not creating blank new stuff and deleting the old stuff.

This is a Django thing, rather than a polymorphic thing, which is why you may be having difficulty finding documentation. The search term that will help you find more help is "django data migrations".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants