迁移

时间:2017-03-23 21:00:00

标签: python django

遵循一些Django教程并得到一个奇怪的错误我无法理解。我之前已经迁移了几次数据库,所以在开始之前我删除了应用程序'migrations'文件夹中的所有文件。这是我的models.py文件:

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):

    user = models.OneToOneField(User, primary_key=True)

    SUBSCRIPTION_PLANS = (
        ('DEMO', 'Free Trial'),
        ('MONTH','Monthly'),
        ('YEAR','Yearly'),
        ('SIXMONTH','Six month plan'),
    )

    profile_pic = models.ImageField(upload_to = 'profile_pics', blank = True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    subscription_plan = models.CharField(max_length=10, choices=SUBSCRIPTION_PLANS, default = 'DEMO')

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

这是view.py文件:

from django.shortcuts import render
from UserProfile.forms import UserForm,UserProfileForm
# Create your views here.
def register(request):

    registered = False

    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(data=request.POST)


        if user_form.is_valid() and  profile_form .is_valid():

            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                print 'has profile pic'
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render(request, 'UserProfile/registration.html',{'user_form':user_form,
                                                            'profile_form':profile_form,
                                                            'registered':registered})

我运行>>python manage.py makemigrations UserProfile并获取:

Migrations for 'UserProfile':
  UserProfile/migrations/0001_initial.py:
    - Create model Profile

这是forms.py:

class UserProfileForm(forms.ModelForm):

    class Meta():
        model = Profile
        fields = ('subscription_plan', 'first_name', 'profile_pic', 'last_name')

我跑:

>>python manage.py migrate UserProfile
Operations to perform:
  Apply all migrations: UserProfile
Running migrations:
  No migrations to apply.

我尝试填写表单,但仍然收到错误:没有这样的列:profile_pic

OperationalError at /user_profile/register
no such column: profile_pic

我清楚地在模型中创建了profile_pic列并进行了迁移。那么为什么Django认为它不存在?

我再次尝试从头开始重新创建表:

python manage.py migrate --fake UserProfile zero
Operations to perform:
  Unapply all migrations: UserProfile
Running migrations:
  Rendering model states... DONE
  Unapplying UserProfile.0001_initial... FAKED

python manage.py makemigrations UserProfile
Migrations for 'UserProfile':
  UserProfile/migrations/0001_initial.py:
    - Create model Profile

python manage.py migrate --fake-initial UserProfile
Operations to perform:
  Apply all migrations: UserProfile
Running migrations:
  Applying UserProfile.0001_initial... FAKED

仍然是同样的错误。 正在运行python manage.py sqlmigrate UserProfile 0001_initial

BEGIN;
--
-- Create model Profile
--
CREATE TABLE "UserProfile_profile" ("user_id" integer NOT NULL PRIMARY KEY REFERENCES "auth_user" ("id"), "profile_pic" varchar(100) NOT NULL, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL, "subscription_plan" varchar(10) NOT NULL);
COMMIT;

到底是什么? 'profile_pic'字段在SQL中!那么什么是错的?

回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/ilyalapan/Documents/Development/OE Ventures/businessMaps/businessMaps/UserProfile/views.py" in register
  26.             profile.save()

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
  796.                        force_update=force_update, update_fields=update_fields)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
  824.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _save_table
  889.                                       forced_update)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _do_update
  939.         return filtered._update(values) > 0

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _update
  654.         return query.get_compiler(self.db).execute_sql(CURSOR)

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1148.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /user_profile/register
Exception Value: no such column: profile_pic

更新:我已经注释掉了模型中的一些字段,但是我会在不同的字段中得到相同的错误。所以,如果它不是'profile_pic',那就是订阅计划。更多信息 - 这是forms.py:

from django import forms
from django.contrib.auth.models import User
from UserProfile.models import Profile

class UserForm(forms.ModelForm):

    password = forms.CharField(widget=forms.PasswordInput())

    #TODO: Haven't figured out why this is needed yet. Will check docs
    class Meta():
        model = User
        fields = ('username', 'email', 'password')

class UserProfileForm(forms.ModelForm):

    class Meta():
        model = Profile
        fields = ('first_name', 'last_name' ,'profile_pic', 'subscription_plan')

我会尝试删除Django应用程序并从头开始。

0 个答案:

没有答案