如何定义模型的字段以从另一个模型的另一个字段中选择另一个应用程序?

发布于 2025-02-13 06:09:15 字数 879 浏览 0 评论 0原文

我有两个不同的应用程序定义了两个模型。 我想为模型中的特定字段提供一个选择选项,该选项是从另一个应用程序中的模型的另一个字段派生的这些属性。

例如:我有一个模型“ 库存”,该模型具有一个字段 'title'

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    ....

我还有另一个模型'发票',它具有字段 'line_one'这两个模型都位于不同的应用中。

class Invoice(models.Model):
   line_one = models.CharField('Line 1', max_length=120)
   ....

我创建了一个用于添加发票详细信息的Django表单。我想要一个选择 line_one 的选择选项,该选项在库存中存储了所有值。标题字段,以便用户只能从下拉菜单中选择这些值。 我该如何实施? 谢谢。

编辑:我将 line_one 引用到 库存模型的字段,但它似乎有效,但并未完全显示属性但显示“库存对象(7)”,“库存对象(8)”

    line_one = models.ForeignKey(Inventory, to_field='title', default=0, on_delete=models.CASCADE)

I have two models defined in two different apps.
I want to have a choice option for a particular field in a model, which is deriving those attributes from another field of a model in another app.

eg: I have a model "Inventory" that has a field 'title'.

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    ....

I also have another model 'Invoice' that has a field 'line_one'. Both these models are located in different apps.

class Invoice(models.Model):
   line_one = models.CharField('Line 1', max_length=120)
   ....

I created a Django form for adding the Invoice details. I want a choice option for line_one that has all the values stored in the Inventory.title field so that the user can only select those values from a dropdown menu.
How can I implement this?
Thanks.

Edit: I referenced line_one to the field title of the Inventory model and it seems to work but it isn't exactly showing the name of the attributes but showing, "Inventory Object(7)", "Inventory Object(8)"

    line_one = models.ForeignKey(Inventory, to_field='title', default=0, on_delete=models.CASCADE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2025-02-20 06:09:15

使用charfield line_one

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    # ...


class Invoice(models.Model):
   line_one = models.CharField('Line 1', max_length=120)
   # ...


class InvoiceForm(forms.ModelForm):
    line_one = forms.CharField(widget=forms.Select)

    class Meta:
        model = Invoice
        fields = ['line_one', ] # ...

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # populate the choices from the Inventory model objects
        self.fields['line_one'].widget.choices = [(i.title, i.title) for i in Inventory.objects.all()]
        # or
        self.fields['line_one'].widget.choices = [(t,t) for t in Inventory.objects.values_list('title', flat=True)

使用foreferkey for line_one

如果您想使用forefer> foreferykey)代码>,有必要具有inting.title的唯一值。

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    # ...

    # The __str()__ method of an object is used by django
    # to generate the labels for a Select input widget
    def __str__(self):
        return self.title


class Invoice(models.Model):
   line_one = models.ForeignKey(Inventory, to_field='title', on_delete=models.CASCADE)
   # ...


class InvoiceForm(forms.ModelForm):
    class Meta:
        model = Invoice
        fields = ['line_one', ] # ...

    # no need for any form adjustments,
    # as django uses a select input as default for ForeignKey fields

Use a CharField for line_one

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    # ...


class Invoice(models.Model):
   line_one = models.CharField('Line 1', max_length=120)
   # ...


class InvoiceForm(forms.ModelForm):
    line_one = forms.CharField(widget=forms.Select)

    class Meta:
        model = Invoice
        fields = ['line_one', ] # ...

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # populate the choices from the Inventory model objects
        self.fields['line_one'].widget.choices = [(i.title, i.title) for i in Inventory.objects.all()]
        # or
        self.fields['line_one'].widget.choices = [(t,t) for t in Inventory.objects.values_list('title', flat=True)

Use a ForeignKey for line_one

If you like to use a ForeignKey, it is necessary to have unique values for Inventory.title.

class Inventory(models.Model):
    title = models.CharField('Title', max_length=120, default='')
    # ...

    # The __str()__ method of an object is used by django
    # to generate the labels for a Select input widget
    def __str__(self):
        return self.title


class Invoice(models.Model):
   line_one = models.ForeignKey(Inventory, to_field='title', on_delete=models.CASCADE)
   # ...


class InvoiceForm(forms.ModelForm):
    class Meta:
        model = Invoice
        fields = ['line_one', ] # ...

    # no need for any form adjustments,
    # as django uses a select input as default for ForeignKey fields
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文