int()参数必须是字符串或数字,而不是'会议'

时间:2012-07-18 10:36:38

标签: python django django-models tornado

考虑这个函数我正在沿着tornadoweb使用Django

def MeetingRecord(userid,mtngid,mesg):
    obj =  Usage()
    obj.name = userid
    obj.meeting_id = mtngid
    obj.action = mesg
  #  obj.participantid = participantid
    obj.save()    

这是模型

class Usage(models.Model):
    user = models.ForeignKey('User',related_name = 'usage_user',null = True)
    meeting = models.ForeignKey('Meeting',related_name = 'meeting_usages',null = True)
    participant= models.ForeignKey('Participant',related_name = 'meeting_participant_id',null = True)
    date = models.DateTimeField(auto_now  = True)
    action = models.CharField(max_length = 500)
    miscellaneous = models.CharField(max_length = 500)

我从像

这样的龙卷风类中调用此方法

MeetingRecord(check_user_exist,mtng,mesg)

但我正在跟踪引用

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.3-py2.7.egg/tornado/web.py", line 1021, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "tornado_main.py", line 383, in post
    MeetingRecord(check_user_exist,mtng,mesg)
  File "tornado_main.py", line 503, in MeetingRecord
    obj.save()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 551, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/manager.py", line 203, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/query.py", line 1576, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/sql/compiler.py", line 909, in execute_sql
    for sql, params in self.as_sql():
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/sql/compiler.py", line 872, in as_sql
    for obj in self.query.objs
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/fields/related.py", line 964, in get_db_prep_save
    connection=connection)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/fields/__init__.py", line 292, in get_db_prep_save
    prepared=False)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/fields/__init__.py", line 284, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/models/fields/__init__.py", line 537, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'Meeting'

我想确认userid是User类的实例,而mtngid是会议类的实例。

请帮我解释为什么我收到此错误。

3 个答案:

答案 0 :(得分:2)

只是一个有根据的猜测:“obj.meeting_id = mtngid”在这里你可能传递了一个会议实例而不是会议ID。

答案 1 :(得分:0)

您的会议是一个外键,它需要一个对象,而不是实际的引用。您也没有meeting_id字段。

试试这个:

def MeetingRecord(userid,mtngid,mesg):
    obj =  Usage()
    obj.name = userid
    obj.meeting = mtngid
    obj.action = mesg
  #  obj.participantid = participantid
    obj.save()

您还应该恰当地命名方法的参数。

答案 2 :(得分:-1)

您使用了obj.meeting_id这是会议'ID字段(AutoField)的外键。 AutoField为IntegerField。这就是错误消息说明 int()参数的原因。在您的情况下,您必须使用obj.meeting_id = mtngid.id。 Burhan的对象引用方式更加清晰。我只建议将mtngid重命名为mtng,因为它不是id,它是对象。