1

我对南方很陌生。我正在处理一个朋友项目,他似乎已经完成了一些迁移。

我有一个模型,我正在尝试向它添加一个额外的 ManyToMany 字段。这是owned_geofences我要添加的字段的类:

class UserProfile(models.Model):

    owned_beacons = models.ManyToManyField(
       Beacon,
       blank=True,
       null=True,
       related_name='owned_beacons'
       )

    #trying to add this one below
    owned_goefences = models.ManyToManyField(
        Geofence,
        blank=True,
        null=True,
        related_name='owned_geofences'
        )

该应用程序名为“个人资料”。首先我这样做:

$ sudo python manage.py schemamigration profile --auto
 + Added M2M table for owned_goefences on profile.UserProfile
Created 0007_auto.py. You can now apply this migration with: ./manage.py migrate profile

酷,它似乎奏效了。然后我这样做了:

$ python manage.py migrate profile
Running migrations for profile:
 - Migrating forwards to 0007_auto.
 > profile:0007_auto
 - Loading initial data for profile.
Installed 0 object(s) from 0 fixture(s)

好的。现在应该工作正常吗?

嗯,它似乎确实奏效了。我进行了一些测试,然后..

>>> user = User.objects.get(pk=1)
<User: nick>
# just to test if user is working properly
>>> user.get_profile().owned_beacons
<django.db.models.fields.related.ManyRelatedManager object at 0x1104e7d50>
# this is the one that isn't working
>>> user.get_profile().owned_geofences
AttributeError: 'UserProfile' object has no attribute 'owned_geofences'

最重要的是,出于好奇,我跑了:

$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.messages
 > django.contrib.staticfiles
 > django.contrib.admin
 > django.contrib.admindocs
 > south

Not synced (use migrations):
 - pp.apps.careers
 - pp.apps.contact
 - pp.apps.geofencemanager
 - pp.apps.locationmanager
 - pp.apps.messagemanager
 - django_extensions
 - pp.profile
 - pp.apps.beaconmanager
(use ./manage.py migrate to migrate these)

为什么这些不同步?我以为我搞砸了,所以我接受了上面的建议并跑了:

$ python manage.py migrate
Running migrations for careers:
- Nothing to migrate.
#etc for all of them

有人可以解释一下这里发生了什么吗?

4

1 回答 1

0

首先,如果你运行syncdb,已经有迁移的应用程序将被忽略,这就是它们不同步的原因。

第二个问题不在南方,它迁移得很好,你应该检查你的数据库,只是为了确保它是否创建了直通表('appname_userprofile_geofence',或类似的东西),我确信它确实如此,因为它说所以: ,如果迁移没有完成,> profile:0007_auto它也会给你一个,不是。DatabaseErrorAttributeError

此外,一切似乎都很好,请确保重新加载 shell,以便它更新了UserProfile模型版本。

于 2014-06-11T09:54:43.277 回答