Django ProgrammingError:使用ManyToManyfield时没有关系

时间:2015-03-22 02:10:16

标签: python django django-models

我正在尝试按用户的可用性过滤我的数据库。使用时间(早上,下午,晚上或下班后)和一天(例如:20)设置可用性

有问题的模型如下:

class Profile(LocationModel):
    # Link with User Account, contains name and creation as well
    user = models.OneToOneField(User)
    avatar = models.BinaryField(null=True)
    # Personal Info
    birthday = models.DateField('birthdate', null=True)
    gender = models.CharField('gender',max_length=15)
    # Conections to other Databases


    def __str__(self): 
        return self.user.username    


User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])

class Availability(models.Model):
     time = models.CharField(max_length = 11)
     day = models.CharField( max_length = 2)
     profile = models.ManyToManyField(Profile)
     def __str__(self):
        return "%s %s" % (self.time , self.day)

要测试我打开shell运行脚本以使用时间/日信息填充数据库,然后执行以下操作:

  a = Availability.objects.all()[0]   # stores a time and days
    profile = Profile.objects.get(id=2) # stores the user
    a.profile.add(profile) 
enter code here
    Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 924, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1028, in _add_items
    new_ids = new_ids - set(vals)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 141, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1202, in iterator
    for row in self.query.get_compiler(self.db).results_iter():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: relation "backend_service_availability_profile" does not exist
LINE 1: ...d_service_availability_profile"."profile_id" FROM "backend_s...

我对django相当新,所以我可能犯了一个简单的错误,但对于我的生活,我无法弄明白。我已经完成了迁移,makemigrations和syncdb,因为它进行了更改,但它仍然会产生相同的错误。我正在使用Django版本1.7谢谢!

0 个答案:

没有答案