使用所选字段添加 m2m 字段
Adding m2m fileds based on selected ones using
我正在尝试将国家 'Latam' 或 'Europe' 转换为国家列表并将其作为国家维护。
例如,当您在国家/地区列表中 select 拉美时,m2m 的结果必须是:[拉美、阿根廷、智利、秘鲁等]
数据库就是这样构建的...我知道最好将其拆分为地区和国家,但我现在无法更改。
这是我做的。我认为这不是解决问题的方法,但也许可以帮助你帮助我。
LATAM = [1, 4, 24]
class PromoManagement(models.Model):
[...]
house_id = models.CharField(max_length=6)
original_title = models.CharField(max_length=150, blank=True, null=True)
country = models.ManyToManyField(Country)
[...]
def save(self, *args, **kwargs):
self.original_title = get_orignal_title(self.house_id)
super().save(*args, **kwargs)
def region_check(sender, instance, action, **kwargs):
if action == 'post_add' or action == 'post_remove':
for country in instance.country.all():
if country.name == 'Latam':
for id in LATAM:
instance.country.add(code)
m2m_changed.connect(region_check, sender=PromoManagement.country.through)
已编辑:可能的解决方案
我设法让它像这样工作:
def latam_region_converter(sender, instance, action, pk_set, **kwargs):
if action == 'post_add' and 95 in pk_set:
# Latam id is 95
instance.country.add(2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
问题是,如果用户手动删除该国家/地区之一,它应该删除拉美。
if action == 'post_remove':
# Then it checks if the country removed is on the list of latam. If true it deletes the id 95 from the list.
我就是这样解决的
LATAM = (2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
def latam_region_converter(sender, instance, action, pk_set, **kwargs):
if action == 'post_add' and 95 in pk_set:
# Latam id is 95
print('Adding latam countries')
instance.country.add(2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
if action == 'post_remove' and any(item in LATAM for item in pk_set):
print('Removing Latam')
instance.country.remove(95)
我正在尝试将国家 'Latam' 或 'Europe' 转换为国家列表并将其作为国家维护。
例如,当您在国家/地区列表中 select 拉美时,m2m 的结果必须是:[拉美、阿根廷、智利、秘鲁等]
数据库就是这样构建的...我知道最好将其拆分为地区和国家,但我现在无法更改。
这是我做的。我认为这不是解决问题的方法,但也许可以帮助你帮助我。
LATAM = [1, 4, 24]
class PromoManagement(models.Model):
[...]
house_id = models.CharField(max_length=6)
original_title = models.CharField(max_length=150, blank=True, null=True)
country = models.ManyToManyField(Country)
[...]
def save(self, *args, **kwargs):
self.original_title = get_orignal_title(self.house_id)
super().save(*args, **kwargs)
def region_check(sender, instance, action, **kwargs):
if action == 'post_add' or action == 'post_remove':
for country in instance.country.all():
if country.name == 'Latam':
for id in LATAM:
instance.country.add(code)
m2m_changed.connect(region_check, sender=PromoManagement.country.through)
已编辑:可能的解决方案
我设法让它像这样工作:
def latam_region_converter(sender, instance, action, pk_set, **kwargs):
if action == 'post_add' and 95 in pk_set:
# Latam id is 95
instance.country.add(2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
问题是,如果用户手动删除该国家/地区之一,它应该删除拉美。
if action == 'post_remove':
# Then it checks if the country removed is on the list of latam. If true it deletes the id 95 from the list.
我就是这样解决的
LATAM = (2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
def latam_region_converter(sender, instance, action, pk_set, **kwargs):
if action == 'post_add' and 95 in pk_set:
# Latam id is 95
print('Adding latam countries')
instance.country.add(2, 62, 6, 3, 4, 45, 103, 36, 42, 104, 43, 34, 44, 7, 46, 37, 75, 35, 58, 38)
if action == 'post_remove' and any(item in LATAM for item in pk_set):
print('Removing Latam')
instance.country.remove(95)