Я пытаюсь использовать электронную почту своей модели пользователя в приложении Маркетинг. Я следовал учебному пособию по маркетингу из видео но в этом видео эта программа не показывает ошибку. когда я писал ту же программу, она показала ошибку:
ValueError в / admin / marketing / marketingpreference / add / слишком много значений для распаковки (ожидается 2)
from django.db import models
from django.db.models.signals import post_save, pre_save
from .utils import Mailchimp
from accounts.models import User
class MarketingPreference(models.Model):
user =models.OneToOneField(User, on_delete=models.CASCADE)
subscribed = models.BooleanField(default=True)
mailchimp_subscribed = models.NullBooleanField(blank=True)
mailchimp_msg= models.TextField(null=True, blank=True)
timestamp =models.DateTimeField(auto_now_add=True)
update =models.DateTimeField(auto_now= True)
def __str__(self):
return self.user.email
def marketing_pref_update_receiver(sender, instance, *args, **kwargs):
if instance.subscribed != instance.mailchimp_subscribed:
if instance.subscribed:
status_code, response_data = Mailchimp().unsubscribe(instance.user.email)
else:
status_code, response_data = Mailchimp().subscribe(instance.user.email)
if response_data['status'] == 'subscribed' :
instance.subscribed = True
instance.mailchimp_subscribed = True
instance.mailchimp_msg = response_data
else:
instance.subscribed = False
instance.mailchimp_subscribed = False
instance.mailchimp_msg = response_data
pre_save.connect(marketing_pref_update_receiver, sender=MarketingPreference)
def make_marketing_pref_receiver(sender, instance, created, *args, **kwargs):
if created:
MarketingPreference.objects.get_or_create(user=instance)
post_save.connect(make_marketing_pref_receiver, sender=User)
и мой utils.py
class Mailchimp(object):
def __init__(self):
super(Mailchimp, self).__init__()
self.key = MAILCHIMP_API_KEY
self.api_url = "https://{dc}.api.mailchimp.com/3.0".format(dc=MAILCHIMP_DATA_CENTER)
self.list_id = MAILCHIMP_EMAIL_LIST_ID
self.list_endpoint = '{api_url}/lists/{list_id}'.format(api_url=self.api_url, list_id=self.list_id)
def get_member_endpoint(self):
return self.list_endpoint + "/members"
def check_valid_status(self, status):
choices = ['subscribed','unsubscribed','cleaned', 'pending']
if status not in choices:
raise ValueError("Not a valid choice for email status")
return status
def add_email(self, email,status = "subscribed"):
self.check_valid_status(status)
data = {
"email_address": email,
"status": status
}
endpoint = self.get_member_endpoint()
r =requests.post(endpoint, auth=("", self.key), data=json.dumps(data))
return r.json()
def subscribe(self, email):
return self.add_email(email)
и мой терминал, показывающий это
[14/Feb/2020 21:15:38] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
Internal Server Error: /admin/marketing/marketingpreference/add/
Traceback (most recent call last):
"D:\software\shopw\marketing\models.py", line 35, in marketing_pref_update_receiver
status_code, response_data = Mailchimp().unsubscribe(instance.user.email)
ValueError: too many values to unpack (expected 2)
, пожалуйста, помогите мне.