AlterField on auto generated _ptr field in migration causes FieldError

2024/10/13 13:14:28

I have two models:

# app1
class ParentModel(models.Model):# some fields

Now, in another app, I have child model:

# app2
from app1.models import ParentModelclass ChildModel(ParentModel):# some fields here too

In initial migration for app2 django creates OneToOneField with parent_link=True named parentmodel_ptr.

Now I want to change this auto generated field to let's say IntegerField, so I create new migration with this operations:

class Migration(migrations.Migration):dependencies = [('app2', '0001_initial'),]operations = [migrations.AlterField('childmodel','parentmodel_ptr',models.IntegerField(null=True, blank=True))]

Trying to migrate, I got an exception

django.core.exceptions.FieldError: Auto-generated field 'parentmodel_ptr' in class 'ChildModel' for parent_link to base class 'ParentModel' clashes with declared field of the same name.

So is that even possible to make it somehow?

Answer

If your code supports it, you could just change the parent class to an abstract class and have all the fields in the child model. However, if you still do need the parent object separately, then I don't think you can change the Django OneToOne link without some serious hacking (not recommended).

If you only need the relation and don't need the methods, etc. then you could remove the inheritance and go with a self-created either OneToOneField or an IntegerField that holds the ForeignKey to that other object. You could elaborate the question with your end goal, so it would be simpler to offer real solutions rather than theories.

https://en.xdnf.cn/q/118077.html

Related Q&A

How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs

import numpy as npthe array looks like so:array = np.zeros((10,10))array = [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0…

Processing items with Scrapy pipeline

Im running Scrapy from a Python script.I was told that in Scrapy, responses are built in parse()and further processed in pipeline.py. This is how my framework is set so far:Python scriptdef script(self…

How to click a button to vote with python

Im practicing with web scraping in python. Id like to press a button on a site that votes an item. Here is the code<html> <head></head> <body role="document"> <div …

Python 2.7 connection to Oracle: loosing (Polish) characters

I connect from Python 2.7 to Oracle data base. When I use:cursor.execute("SELECT column1 FROM table").fetchall()]I have got almost proper values for column1 because all Polish characters (&qu…

getting friendlist from facebook graph-api

I am trying to get users friend list from facebook Graph-api. So after getting access token when I try to open by urlopen byhttps://graph.facebook.com/facebook_id/friends?access_token=authentic_access…

Sorting Angularjs ng-repeat by date

I am relatively new to AngularJS. Could use some helpI have a table with the following info<table><tr><th><span ng-click="sortType = first_name; sortReverse = !sortReverse&quo…

Html missing when using View page source

Im trying to extract all the images from a page. I have used Mechanize Urllib and selenium to extract the Html but the part i want to extract is never there. Also when i view the page source im not abl…

Move file to a folder or make a renamed copy if it exists in the destination folder

I have a piece of code i wrote for school:import ossource = "/home/pi/lab" dest = os.environ["HOME"]for file in os.listdir(source):if file.endswith(".c")shutil.move(file,d…

Segmentation fault after removing debug printing

I have a (for me) very weird segmentation error. At first, I thought it was interference between my 4 cores due to openmp, but removing openmp from the equation is not what I want. It turns out that wh…

numpy get 2d array where last dimension is indexed according to a 2d array

I did read on numpy indexing but I didnt find what I was looking for.I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im.Wit…