Answer the question
In order to leave comments, you need to log in
ForeignKey to specific model field, how?
Gentlemen programmers, tell me how can I make the oneToMany relation through ForeignKey, binding not to a specific model, but to its field?
class TechType(models.Model):
type_name = models.CharField(max_length=100, verbose_name='Вид техники', unique=True)
def __str__(self):
return self.type_name
class TechUnit(models.Model):
inner_id = models.CharField(max_length=100, verbose_name='Внутренний номер', unique=True)
tech_type = models.ForeignKey(TechType, on_delete=models.CASCADE, verbose_name='Вид техники')
def __str__(self):
return self.inner_id
Answer the question
In order to leave comments, you need to log in
You have trouble understanding relationships, ForeignKey is bound to model id. Your wishlist doesn't make sense.
Or if there are not many dozens of class objects - two! In general, eliminate the TechType model
and in the TechUnit model, fill in the tech_type field through choises
class TechUnit(models.Model):
type_name1 = 'номер1'
type_name2 = 'номер2'
type_name3 = 'номер3'
Category = [
( type_name1, 'номер1'),
( type_name2, 'номер2'),
( type_name3, 'номер3'),
]
inner_id = models.CharField(max_length=100, verbose_name='Внутренний номер', unique=True)
tech_type = models.CharField(max_length=100, verbose_name='Вид техники', choices=Category,
default=Main)
def __str__(self):
return self.inner_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question