2016-09-04 4 views
0

上のデータを表示することができない私が働いているモデルです。Djangoは - ここではDjangoの管理ページ

class Customer(models.Model): 
    customer_id = models.AutoField(primary_key=True, unique=True) 
    full_name = models.CharField(max_length=50) 
    user_email = models.EmailField(max_length=50) 
    user_pass = models.CharField(max_length=30) 

    def __str__(self): 
     return "%s" % self.full_name 

class CustomerDetail(models.Model): 
    phone_regex = RegexValidator(regex = r'^\d{10}$', message = "Invalid format! E.g. 4088385778") 
    date_regex = RegexValidator(regex = r'^(\d{2})[/.-](\d{2})[/.-](\d{2})$', message = "Invalid format! E.g. 05/16/91") 

    customer = models.OneToOneField(
     Customer, 
     on_delete=models.CASCADE, 
     primary_key=True, 
    ) 
    address = models.CharField(max_length=100) 
    date_of_birth = models.CharField(validators = [date_regex], max_length = 10, blank = True) 
    company = models.CharField(max_length=30) 
    home_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True) 
    work_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True) 

    def __str__(self): 
     return "%s" % self.customer.full_name 

ここforms.pyです:

from django.forms import ModelForm 

from .models import CustomerDetail 

class CustomerDetailForm(ModelForm): 
    class Meta: 
     model = CustomerDetail 
     fields = ['address', 'date_of_birth', 'company', 'home_phone', 'work_phone',] 

私は(自分のアプリケーションのビューを持っていますユーザーがログインした後)、create_profileと呼ばれ、ユーザーに詳細を尋ねて、それを実装するためにModelFormインスタンスを使用しました。

def create_profile(request): 
if request.POST: 
    form = CustomerDetailForm(request.POST) 
    if form.is_valid(): 

     address = form.cleaned_data['address'] 
     date_of_birth = form.cleaned_data['date_of_birth'] 
     company = form.cleaned_data['company'] 
     home_phone = form.cleaned_data['home_phone'] 
     work_phone = form.cleaned_data['work_phone'] 

     profdata = CustomerDetail(address = address, date_of_birth = date_of_birth, company = company, home_phone = home_phone, work_phone = work_phone) 
     profdata.save() 

     return render(request, 'newuser/profile_created.html', {form: form}) 
else: 
    return redirect(create_profile) 

私は、対応するテンプレートHTMLのフォームを記入し、提出ヒット、それは私に連続したページを示しているが、私は管理ページにCustomerDetailエントリをチェックしたときに、私は以下を参照してください。ここviews.pyからの抜粋です実際のレコードの代わりに ' - '私はここでどこが間違っていますか? clean()メソッドをオーバーライドする必要がありますか?助けてください。ありがとう!

答えて

0

あなたのケースではcleaned_dataを無効にする必要はありません。すでにModelFormを使用しているので、saveメソッドの後にCustomerDetailインスタンスを作成します。
ビューは次のようになります。

def create_profile(request): 
    if request.method == 'POST': 
     form = CustomerDetailForm(request.POST) 
     if form.is_valid(): 
      form.save() 
      return render(request, 'newuser/profile_created.html', {'form': form}) 
    else: 
     form = CustomerDetailForm() 
    return render(request, 'path_to_create_profile.html', {'form': form}) 

check the docs about forms

+0

をあなたはここで他の部分で何が起こっているのか説明できますか? –

+0

else形式の@KrithikaRaghavendranは 'GET'レスポンスのために空です –

+0

' form.save() 'が機能していない、データレコードが管理ページに表示されない –