在 Django 3.2 中,如何使用数组中的值初始化构造函数中的 ManyToMany 字段?

In Django 3.2, how do I intialize my ManyToMany field in my consturctor with values from an array?

我正在使用 Python 3.9 和 Django 3.2。我有这个带有 ManyToMany 字段的模型 ...

class Account(models.Model):    
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    active = models.BooleanField(default=True)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    vendor = models.ForeignKey(Vendor, on_delete=models.DO_NOTHING)
    crypto_currencies = models.ManyToManyField(CryptoCurrency)

    def __init__(self, user, vendor, crypto_currencies_arr, max_transactions=DEFAULT_MAX_BUY_TRANSACTIONS):
        self.crypto_currencies.set( set() )
        for cc in crypto_currencies_arr:
            self.crypto_currencies.add(cc)
        super().__init__(
            user=user,
            vendor=vendor,
            crypto_currencies=self.crypto_currencies
        )

我无法弄清楚如何使用数组初始化我的多对多字段。以上导致此错误

>>> account = Account(user=u, vendor=v, crypto_currencies_arr=[c])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    account = Account(user=u, vendor=v, crypto_currencies_arr=[c])
  File "/Users/davea/Documents/workspace/cbapp/cbapp/models/account.py", line 47, in __init__
    self.crypto_currencies.set( set() )
  File "/Users/davea/Documents/workspace/cbapp/venv/lib/python3.9/site-packages/django/db/models/fields/related_descriptors.py
", line 536, in __get__
    return self.related_manager_cls(instance)
  File "/Users/davea/Documents/workspace/cbapp/venv/lib/python3.9/site-packages/django/db/models/fields/related_descriptors.py
", line 846, in __init__
    self.core_filters[core_filter_key] = getattr(instance, rh_field.attname)
  File "/Users/davea/Documents/workspace/cbapp/venv/lib/python3.9/site-packages/django/db/models/query_utils.py", line 142, in
 __get__
    val = self._check_parent_chain(instance)
  File "/Users/davea/Documents/workspace/cbapp/venv/lib/python3.9/site-packages/django/db/models/query_utils.py", line 158, in
 _check_parent_chain
    return getattr(instance, link_field.attname)
AttributeError: 'NoneType' object has no attribute 'attname'

如何使用数组中的值正确设置多对多字段?

编辑:根据给出的建议,我尝试创建一个单独的方法来创建帐户,然后创建集合,但是这个

def create_account(self, user, vendor, crypto_currencies_arr=[], max_transactions=DEFAULT_MAX_BUY_TRANSACTIONS):
    
    a = Account(
        user=user,
        vendor=vendor,
        max_transactions=max_transactions
    )
    a.save()
    cc_set = set()
    for cc in crypto_currencies_arr:
        cc_set(cc)
    a.crypto_currencies = cc_set
    a.save()

导致错误

cc_set(cc)
TypeError: 'set' object is not callable

根据您的编辑,如果 crypto_currencies_arr 包含带有主键的 CryptoCurrency 个实例列表,那么您可以这样做:

def create_account(self, user, vendor, crypto_currencies_arr=[], max_transactions=DEFAULT_MAX_BUY_TRANSACTIONS):
    a = Account.objects.create(
        user=user,
        vendor=vendor,
        max_transactions=max_transactions,
    )
    a.crypto_currencies.set(crypto_currencies_arr)