我怎样才能像 manytomanyfield 一样在外键数据的 django 管理下拉列表中进行多选?
How can i multiselect in django admin dropdown on foreignkey data just like manytomanyfield?
models.py
from django.db import models
class Images(models.Model):
def upload_path(instance, filename):
return '/images/'.join([filename])
image = models.ImageField(upload_to=upload_path, blank=True, null=True)
logits = models.BinaryField()
#customer = models.ForeignKey(Customer ,on_delete=models.DO_NOTHING, default=None)
class Customer(models.Model):
customer_id = models.BigIntegerField(unique=True)
first_name = models.CharField(max_length=300)
last_name = models.CharField(max_length=300)
images = models.ForeignKey(Images ,on_delete=models.DO_NOTHING, default=None)
def __str__(self):
return str(self.customer_id)
我的问题是我希望能够将多个图像分配给单个用户,这应该是可能的,因为 ForeignKey 但我似乎无法像那样工作。
我想要多选字段,就像在 manytomanyfield 中一样,但在外键字段中。
My problem is [I] want to be able to assign multiple images to [a] single user which should be possible because of ForeignKey
but [I] don't seem to get it to work like that.
否:一个ForeignKey
是一个many-to-one关系:这意味着 many Customer
s 可以引用一个,可能是相同的,单个 Images
。如果你想使用多个 Image
,那么你需要将 ForeignKey
写在相反的方向,所以:
from django.db import models
class Image(models.Model):
def upload_path(instance, filename):
return f'/images/{filename}'
image = models.ImageField(upload_to=upload_path, blank=True, null=True)
logits = models.BinaryField()
<strong>customer</strong> = models.ForeignKey(
<strong>'Customer'</strong>,
on_delete=models.SET_NULL,
null=True,
<strong>related_name='images'</strong>
)
class Customer(models.Model):
customer_id = models.BigIntegerField(unique=True)
first_name = models.CharField(max_length=300)
last_name = models.CharField(max_length=300)
def __str__(self):
return f'{self.customer_id}'
models.py
from django.db import models
class Images(models.Model):
def upload_path(instance, filename):
return '/images/'.join([filename])
image = models.ImageField(upload_to=upload_path, blank=True, null=True)
logits = models.BinaryField()
#customer = models.ForeignKey(Customer ,on_delete=models.DO_NOTHING, default=None)
class Customer(models.Model):
customer_id = models.BigIntegerField(unique=True)
first_name = models.CharField(max_length=300)
last_name = models.CharField(max_length=300)
images = models.ForeignKey(Images ,on_delete=models.DO_NOTHING, default=None)
def __str__(self):
return str(self.customer_id)
我的问题是我希望能够将多个图像分配给单个用户,这应该是可能的,因为 ForeignKey 但我似乎无法像那样工作。
我想要多选字段,就像在 manytomanyfield 中一样,但在外键字段中。
My problem is [I] want to be able to assign multiple images to [a] single user which should be possible because of
ForeignKey
but [I] don't seem to get it to work like that.
否:一个ForeignKey
是一个many-to-one关系:这意味着 many Customer
s 可以引用一个,可能是相同的,单个 Images
。如果你想使用多个 Image
,那么你需要将 ForeignKey
写在相反的方向,所以:
from django.db import models
class Image(models.Model):
def upload_path(instance, filename):
return f'/images/{filename}'
image = models.ImageField(upload_to=upload_path, blank=True, null=True)
logits = models.BinaryField()
<strong>customer</strong> = models.ForeignKey(
<strong>'Customer'</strong>,
on_delete=models.SET_NULL,
null=True,
<strong>related_name='images'</strong>
)
class Customer(models.Model):
customer_id = models.BigIntegerField(unique=True)
first_name = models.CharField(max_length=300)
last_name = models.CharField(max_length=300)
def __str__(self):
return f'{self.customer_id}'