I have two models, Account
model & Thread
model:
JavaScript
x
24
24
1
class Account(AbstractBaseUser, PermissionsMixin):
2
3
class Meta:
4
verbose_name_plural = "Account List"
5
6
email = models.EmailField(max_length=255, unique=True)
7
username = models.CharField(max_length=255, unique=True)
8
name = models.CharField(max_length=255, default="")
9
profile_image = models.ImageField(max_length=255, upload_to=profile_image_path, blank=True, null=True, unique=True)
10
about = models.TextField(max_length=255, default='Write something about yourself...', blank=True)
11
start_date = models.DateTimeField(default=timezone.now)
12
is_staff = models.BooleanField(default=False)
13
is_superuser = models.BooleanField(default=False)
14
is_active = models.BooleanField(default=True)
15
last_login = models.DateTimeField(auto_now=True)
16
17
objects = AccountManager()
18
19
USERNAME_FIELD = "email"
20
REQUIRED_FIELDS = ["username", "name"]
21
22
def __str__(self):
23
return self.username
24
JavaScript
1
11
11
1
class Thread(models.Model):
2
3
options = (('active', 'Active'), ('deactivated', 'Deactivated'))
4
5
username = models.ForeignKey(Account, on_delete=models.CASCADE, to_field='username')
6
alt = models.TextField(max_length=255, blank=True)
7
image = models.ImageField(max_length=255, upload_to=thread_image_path, blank=True)
8
content = models.TextField(blank=True)
9
created = models.DateTimeField(blank=True, null=True, default=timezone.now)
10
status = models.CharField(max_length=11, choices=options, default='active')
11
If I have already created a thread that is ForeignKey
to the Account
model, I am not able to change the username
of the Account
model, returning the error FOREIGN KEY constraint failed
. I guess the existing Thread
model require a username to point to. Is there way to create a custom update
method in view.py
to update the ForeignKey
automatically?
Here is my view.py
:
JavaScript
1
5
1
class UserViewSet(viewsets.ModelViewSet):
2
serializer_class = UserSerializer
3
queryset = Account.objects.all()
4
permission_classes = (AllowAny,)
5
EDIT: serializer.py
JavaScript
1
21
21
1
class ThreadSerializer(serializers.ModelSerializer):
2
3
profile_image = serializers.SerializerMethodField('get_profile_image')
4
created = serializers.DateTimeField(format="%d %B, %Y %H:%M:%S")
5
6
class Meta:
7
model = Thread
8
fields = (
9
'id',
10
'username',
11
'profile_image',
12
'alt',
13
'image',
14
'content',
15
'created',
16
'status')
17
18
def get_profile_image(self, thread):
19
profile_image_url = thread.username.profile_image.url
20
return profile_image_url
21
Error:
JavaScript
1
22
22
1
IntegrityError at /account/auth/user/1/
2
FOREIGN KEY constraint failed
3
Request Method: PUT
4
Request URL: http://127.0.0.1:8000/account/auth/user/1/
5
Django Version: 4.0.4
6
Exception Type: IntegrityError
7
Exception Value:
8
FOREIGN KEY constraint failed
9
Exception Location: c:Users85291Desktopvscodemy-appwebenvlibsite-packagesdjangodbbackendssqlite3base.py, line 477, in execute
10
Python Executable: c:Users85291Desktopvscodemy-appwebenvScriptspython.exe
11
Python Version: 3.10.2
12
Python Path:
13
['C:\Users\85291\Desktop\vscode\my-app\web\jtravel',
14
'c:\Users\85291\.vscode\extensions\ms-python.python-2022.6.3\pythonFiles\lib\python\debugpy\_vendored\pydevd',
15
'C:\Python310\python310.zip',
16
'C:\Python310\DLLs',
17
'C:\Python310\lib',
18
'C:\Python310',
19
'c:\Users\85291\Desktop\vscode\my-app\web\env',
20
'c:\Users\85291\Desktop\vscode\my-app\web\env\lib\site-packages']
21
Server time: Sun, 05 Jun 2022 16:40:53 +0800
22
Advertisement
Answer
delete to_field='username'
JavaScript
1
2
1
username = models.ForeignKey(Account, on_delete=models.CASCADE)
2
it is a reason of an error