Plugin: ForeignKey array? #10046
-
Hope someone can clarify this for me, Model: class System(NetBoxModel):
servers = models.ForeignKey(
to='virtualization.VirtualMachine',
on_delete=models.PROTECT,
related_name='+',
blank=True,
null=True
)
#... Form: class SystemForm(NetBoxModelForm):
servers = DynamicModelMultipleChoiceField(
queryset=VirtualMachine.objects.all()
) But with this approach its adding a list of virtualmachine elements. Thank you 😁 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
A foreign key is a database field which references a value in some other table; it can only reference a single object. To represent relationships to multiple objects, Django provides ManyToManyField, which works by constructing an intermediate table mapping objects on either side of the relationship. |
Beta Was this translation helpful? Give feedback.
-
Use a ManyToMany field.
…Sent from my iPhone
On 17 Aug 2022, at 12:02, ZuperSero ***@***.***> wrote:
Hope someone can clarify this for me,
I want to make a form the can add multiple virtual machines as ForeignKeys.
Model:
class System(NetBoxModel):
servers = models.ForeignKey(
to='virtualization.VirtualMachine',
on_delete=models.PROTECT,
related_name='+',
blank=True,
null=True
)
#...
Form:
class SystemForm(NetBoxModelForm):
servers = DynamicModelMultipleChoiceField(
queryset=VirtualMachine.objects.all()
)
But with this approach its adding a list of virtualmachine elements.
Is there a way to make the model key an array of ForeignKeys?
Thank you 😁
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
|
Beta Was this translation helpful? Give feedback.
A foreign key is a database field which references a value in some other table; it can only reference a single object. To represent relationships to multiple objects, Django provides ManyToManyField, which works by constructing an intermediate table mapping objects on either side of the relationship.