1๐
โ
You can use GenericForeignKey, from the docs
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def __str__(self):
return self.tag
And then:
>>> from django.contrib.auth.models import User
>>> guido = User.objects.get(username='Guido')
>>> t = TaggedItem(content_object=guido, tag='bdfl')
>>> t.save()
>>> t.content_object
<User: Guido>
But this solution maybe problematic in the future.
What about simpler solution
- Make profession as choice field
- Put your mana, strength, etc in UserProfile model
- Dependently on choice set proper value
You can overwrite the save method and if user choice Warrior set strength on 20, etc.
๐คrzych
0๐
Check this answer in similar case.
Based on this you can define:
class UserProfile(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, related_name='profile'
)
profession = models.ForeignKey(WarriorClass)
๐คGiaMele
- Django + postgres: orderby + distinct + filter() โ strange behaviour
- Getting error in mixins when I try to login as a user
- Getting metadata from links using BeautifulSoup
Source:stackexchange.com