2012-01-26 3 views
0

バックエンドとしてMySQLを使用するDjangoアプリケーションがあります。生のMySQLレコードが一つの価値を示すのは難しいですが、DjangoはWebアプリケーションに何か他のものを提示しています。DjangoとMySQL - 2つのデータが異なる

たとえば、クライアントデータ用のテーブルがあります。各レコードのフィールドの1つはsnailMailInvoiceと呼ばれ、Y/N選択です - デフォルトはY(varcharタイプ)です。

+-------------------+--------------+------+-----+---------+----------------+ 
| Field    | Type   | Null | Key | Default | Extra   | 
+-------------------+--------------+------+-----+---------+----------------+ 
| snailMailInvoice | varchar(3) | NO |  | Y  |    | 

生のMySQL:

select * from systems_system_contact where lastName="SomeClient"; 
...a bunch of other fields... | snailMailInvoice | 
...a bunch of other fields... | N 

すると、Djangoのアプリケーションで、フォームが表示さY(他に選択肢)。これは、DjangoアプリケーションのMySQL値を見ることができないので、デフォルトでYになります。 DjangoアプリケーションでNを選択してフォームを保存した場合、その値はDjangoのNに固執します。

なぜこれが起こっていますか?

EDIT - いくつかのコードを追加する

Forms.py:

class System_Contact_Form(ModelForm): 
    class Meta: 
     model = System_Contact 
     exclude = ('isMainContact', 'systemOwner', 'companyName', 'isRessyContact') 

Views.py:

def contact_details(request, scID): 
    redirect_to = request.REQUEST.get('next', '/systems/contacts/') 
    if request.method == 'POST': 
     syscontEdit = System_Contact.objects.get(pk=scID) 
     form = System_Contact_Form(request.POST, instance=syscontEdit) 

     if form.is_valid(): 
      form.save() 
      return HttpResponseRedirect(redirect_to) 
    else: 
     syscontView = System_Contact.objects.get(pk=scID) 
     form = System_Contact_Form(instance=syscontView) 

    c = { 
     'form':form, 
     'cancel':redirect_to 
     } 

    return render_to_response('pages/systems/contact_details.html', c, context_instance=RequestContext(request)) 

Models.py:

class System_Contact(models.Model): 
IS_MAIN_CONTACT_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

IS_SYSTEM_OWNER_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

IS_RESSY_CONTACT_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No, this is a commercial contact'), 
) 

TRADE_CHOICES = (
    ('EL', 'Electrician'), 
    ('LA', 'Landscaper'), 
    ('PL', 'Plumber'), 
    ('TR', 'Trencher'), 
) 

SNAIL_MAIL_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

SNAIL_MAIL_INVOICE_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 


firstInitial = models.CharField(max_length = 10, verbose_name = 'First Initial', blank = True, null = True) 
firstName = models.CharField(max_length = 60, verbose_name = 'First Name', blank = True, null = True) 
lastName = models.CharField(max_length = 160, verbose_name = 'Last Name', blank = True, null = True) 
phonetically = models.CharField(max_length = 100, verbose_name = 'Phonetically', blank = True, null = True) 
companyName = models.CharField (max_length = 160, verbose_name = 'Company Name', blank = True, null = True) #Only used for Commercial Owners, no other field needed 
homePhone = models.CharField(max_length = 60, verbose_name = 'Home Phone Number', blank = True, null = True) 
officePhone = models.CharField(max_length = 60, verbose_name = 'Office Phone Number', blank = True, null = True) 
cellPhone = models.CharField(max_length = 60, verbose_name = 'Cell Phone Number', blank = True, null = True) 
faxNumber = models.CharField (max_length= 60, blank=True, null=True, verbose_name = 'Fax Number') 
isMainContact = models.CharField (max_length = 3, verbose_name = 'Is the Main Contact?', choices = IS_MAIN_CONTACT_CHOICES, default='N') 
isRessyContact = models.CharField (max_length = 3, verbose_name = 'Is this a Ressy Contact?', choices = IS_RESSY_CONTACT_CHOICES, default='Y') 
isArchived = models.BooleanField(verbose_name = 'Archived?', default = False) 
systemOwner = models.CharField (max_length = 3, verbose_name = 'Is a System Owner?', choices = IS_SYSTEM_OWNER_CHOICES, default='N') #this is just a flag to say they own a system 
worksFor = models.CharField (max_length = 70, verbose_name = 'Works For', blank = True, null = True) 
tradeType = models.ForeignKey(Contact_Trade, blank=True, null=True, verbose_name='Trade') 
emailAddress = models.EmailField(verbose_name = 'Email Address', blank = True, null = True) 

billingAddress = models.CharField(max_length = 150, verbose_name = 'Billing Address', blank=True, null=True) 
billingCity = models.CharField(max_length = 90, verbose_name = 'Billing City', blank=True, null=True) 
billingProvince = models.CharField(max_length = 30, verbose_name = 'Billing Province', blank=True, null=True) 
billingPostalCode = models.CharField(max_length = 10, verbose_name = 'Billing Postal Code', blank=True, null=True) 
snailMailOnly = models.CharField(max_length = 3, verbose_name = 'Snail Mail Only?', choices = SNAIL_MAIL_CHOICES, default='Y') 
snailMailInvoice = models.CharField(max_length = 3, verbose_name = 'Snail Mail Invoice?', choices = SNAIL_MAIL_INVOICE_CHOICES, default='Y') 
+0

フォームを呼び出す 'views.py'にいくつかのコードを提供できますか?そしてそれぞれの 'forms.py'? –

+0

デバッグする最も簡単な方法は、クエリーセットで生成されたSQL文を出力し、データベースに対して手動で実行し、それが何を返すかを確認することです。 – akonsu

+0

@akonsu - これを実行して、データベースが正しい値を示していることがわかりましたが、 'shell'で実行されたクエリーは' u'N \ r''の値を示します。私はデータがどのようにインポートされているかに問題があると推測していますか? (私はCSVとして保存してデータをインポートしてExcelファイルからデータを移動しています) – Garfonzo

答えて

0

OK - 私はそれを理解した。

私のCSVファイルには、snailMailInvoiceフィールドが各行の最後のフィールドであるという事実に関連するものがあることに気付きました。したがって、行末には復帰があります。私はこれが\nであると仮定しました。したがって、私のMySQLコマンドでCSVをインポートするには、terminated by '\n'と書いてあります。

しかし、MySQLはすべての行で '\ r'を取り出し、snailMailInvoiceフィールドに追加していました。したがって、すべてのレコードは、\rが添付されたYまたはNのいずれかを持ちます。

私はMySQLのインポートステートメントを次のように修正しました:lines terminated with '\r\n'これですべてが正常に動作しています。

レッスンを受けました。

ありがとうございました。

+0

あなたのフィールドはYまたはNのどちらでも簡単にBooleanFieldとして実装できます。 – akonsu

+0

確かに - これは私が最初にDjangoを学んだときに実装されたモデルです。私はたくさんのモデル/データを整理しています。ありがとう。 – Garfonzo

関連する問題