0

这是我的models.py

class ShopifyUserProfile(models.Model):
    shop_user = models.ForeignKey(UserProfile) 
    ....

我正在尝试将其对象保存在 views.py

  shop=shopify.Shop.current()
  saved = ShopifyUserProfile.objects.get_or_create(shop_user.user = shop.attributes['shop_owner'], shop_name = shop.attributes['name'],.... etc )

当我试图保存它时会弹出一个错误

*** ValueError: invalid literal for int() with base 10: 'somevalue'

我试过了 :

temp = shop.attributes['shop_owner']
temp=temp.strip()
temp=str(temp)

仍然得到同样的错误。

更新:它可能是 shop_user 是一个外键,我们可以明确分配。

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    subscription = models.ForeignKey(Subscription)

为此,我什至尝试过:

ShopifyUserProfile.objects.get_or_create(shop_user.user = temp)

它弹出新的错误:

*** SyntaxError: keyword can't be an expression (<stdin>, line 1)

我哪里错了??

更新(根据 ans 更正,即现在传递对象)但仍然出现相同的错误:

subs, stat = Subscription.objects.get_or_create(validity=datetime.now()+timedelta(days=30))
user, stat = UserProfile.objects.get_or_create(user=shop.attributes['shop_owner'],subscription=subs)

saved = ShopifyUserProfile.objects.get_or_create(shop_user =user ,shop_name = shop.attributes['name'],...

错误 :

ipdb> user, stat = UserProfile.objects.get_or_create(user=shop.attributes['shop_owner'],subscription=subs)
*** ValueError: invalid literal for int() with base 10: 'Jofin Joseph'
4

1 回答 1

1

您需要将User对象或User.id值传递给您的ShopifyUserProfile.get_or_create调用。

在您的情况下,您传入一个字符串,然后将其传递给,int因为 Django 需要一个整数。您要么必须为其创建一个对象,要么事先从数据库中User检索相关对象。User

User如果您有对象,您的调用应如下所示:

ShopifyUserProfile.objects.get_or_create(user=user_object, ...)
于 2013-05-13T14:39:17.020 回答