从Django模型中的外键获取字段的值

huangapple go评论69阅读模式
英文:

Get value of a field from a foreigKey in Django Models

问题

现在,我有两个在我的Django数据库中的类:

class Test(models.Model):
    sequenzaTest = models.ForeignKey("SequenzaMovimento", null=True, on_delete=models.SET_NULL)
    
class SequenzaMovimento(models.Model):
    nomeSequenza = models.CharField(max_length=50, blank=False, null=False)
    serieDiMovimenti = models.TextField(blank=False, null=False, default="")

现在,每个Test对象都可以关联到一个SequenzaMovimento对象。不同的Test对象可以关联到相同的SequenzaMovimento对象。

现在,我知道我的Test对象的主键。如何找到与Test对象关联的SequenzaMovimento对象中的serieDiMovimenti

我可以从Test对象中获取sequenzaTest

testo_sequenza = Test.objects.get(pk=idObj)
testo_sequenza.sequenzaTest

但是我不知道如何访问serieDiMovimenti

英文:

I have two classes in my Database in Django

class Test(models.Model):
    sequenzaTest = models.ForeignKey("SequenzaMovimento", null=True, on_delete=models.SET_NULL)

class SequenzaMovimento(models.Model):
    nomeSequenza = models.CharField(max_length=50, blank=False, null=False)
    serieDiMovimenti = models.TextField(blank=False, null=False, default="")

Now, every Test object created can be associated with just one SequenzaMovimento object. Different Test objects can have the same SequenzaMovimento

Now, I know the primary key of my Test. How do I find the serieDiMovimenti inside the SequenzaMovimento object which is linked to the Test object?

I can get the sequenzaTest from the Test object with

testo_sequenza = Test.objects.get(pk=idObj)
testo_sequenza.sequenzaTest

but I can't find to understand how access serieDiMovimenti

答案1

得分: 1

testo_sequenza = Test.objects.get(pk=idObj)
testo_sequenza.sequenzaTest.serieDiMovimenti

英文:

You could try:

testo_sequenza = Test.objects.get(pk=idObj)
testo_sequenza.sequenzaTest.serieDiMovimenti

答案2

得分: 1

这应该可以工作:

try:
    testo_sequenza = Test.objects.get(pk=idObj)
except Test.DoesNotExist:
    # 如果未找到,可以在这里执行程序需要执行的操作。如果在循环中,可以考虑使用 return 或 continue/break
    print("Testo Sequenza not found")
sequenza_test = testo_sequenza.sequenzaTest
serie_di_movimenti = sequenza_test.serieDiMovimenti

此外,您还应该检查以下代码:

serieDiMovimenti = models.TextField(blank=False, null=False, default="")

因为如果您希望它是一个"可选字段",那么应该是 blank=True

英文:

This should work:

try:
    testo_sequenza = Test.objects.get(pk=idObj)
except Test.DoesNotExist:
    # do here what you need the program to do if not found. maybe a return if function or a continue/break if you're in a loop
    print("Testo Sequenza not found")
sequenza_test = testo_sequenza.sequenzaTest
serie_di_movimenti = sequenza_test.serieDiMovimenti

Also you should check

serieDiMovimenti = models.TextField(blank=False, null=False, default="")

Because if you want it to be "optional field" then it would be blank=True

huangapple
  • 本文由 发表于 2023年6月1日 23:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定