On Mon, Oct 24, 2011 at 1:11 PM, Jani Tiainen <[email protected]> wrote:

> Hi,
>
> It really depends what is the result you're after.
>

What I'm really after is such that whenever a transaction is edited and the
branch name on that
transaction is changed, the model Transaction should save the changed value
in the database.

>
> Since your post doesn't indicate I made two guesses:
>
> 1) You want to store transaction in denormalized form. Meaning that branch
> name will be stored as a string without any relation to existing data.
>

It will have relation to Branch model name_branch field. So a transaction
just lookup the branch name from Branch model. This is the reason I made it
a ForeignKey.

>
> This is good for history record keeping so if branches do change, value
> stored in transaction is kept unchanged. I assume that this is also your
> case.
>
> Trick there is to define branch as a charfield, not foreign key.
>
> class Transaction(models.Model):
>      branch_name = models.CharField(max_length=**255)
>
> And in a form you use form.ChoiceField (since it will be simple list of
> choices, not list of model instances)
>

If I use form.ChoiceField, how do I get the records from the database? The
choices are dynamic, which is why I have opted to go for ModelChoiceField.
Also, I don't really want to care if the ForeignKey field in Transaction
model is of type int or varchar, since I see that Mysql doesn't care about
this either. My major concern is why is Django reporting the error:

<ul class="errorlist"><li>branch_n
ame<ul class="errorlist"><li>Select a
valid choice.
 That choice is not one of the available choices.</li></ul></li></ul>

Hence, saying the form is invalid? Is there any special thing I'm missing. I
have tried
to check the admin module source to see how it is done in there, but I've
not been able
to see much.

Please, what do I do in this case?

>
> 2) You want to use character field as a primary key.
>
> This is very easy and doable, though it will kill relational queries quite
> easily - strings do not make great indexes.
>
> Just change your primary key to be something like:
>
> class Branch(models.Model):
>    name = models.CharField(max_length=**255, primary_key=True)
>
> This approach has it's caveats specially when removing/renaming entries. I
> wouldn't suggest this approach.


This is not the issue in this use case.

Thanks for the help.

>
>
> On 24.10.2011 13:14, Kayode Odeyemi wrote:
>
>> Hello all,
>>
>> Is there a way to have a foreign key on a model which uses a type other
>> than Django's forceful
>> int type to make the storage for a ModelChoiceField widget?
>>
>> I have something like this I'm working with:
>>
>> forms.py
>> ------------
>>
>> class BranchModelChoiceField(**ModelChoiceField):
>>     def label_from_instance(self, obj):
>>         return obj
>>
>> class TransactionUpdateForm(forms.**ModelForm):
>>     branchqs = Branch.objects.values_list(u'**name_branch', flat=True) #
>> use flat=True if values_list is called with a single field
>>     branch_name = BranchModelChoiceField(**branchqs)
>>     class Meta:
>>         model = Transaction
>>         fields = ('teller_no', 'paid')
>>
>> models.py
>> --------------
>> class Branch(models.Model):
>> """ Branch """
>>     bid = models.AutoField(primary_key=**True)
>>     name_branch = models.CharField(max_length=**255)
>>
>>     class Meta:
>>         db_table = u'branch'
>>
>>     def __unicode__(self):
>>         return self.name_branch
>>
>> class Transaction(models.Model):
>>     id = models.AutoField(primary_key=**True)
>>     branch_name = models.ForeignKey(Branch, blank=True, null=True,
>> related_name='transaction')
>>     paid = models.BooleanField(default=**False)
>>     teller_no = models.CharField(max_length=**20, blank=True)
>>     created = models.DateTimeField(auto_now_**add=True)
>>     updated = models.DateTimeField(auto_now=**True)
>>
>> in views.py
>> ---------------
>> if request.POST:
>>         update_form = TransactionUpdateForm(request.**POST, instance=txn)
>>         txn.branch_name = Branch([int(k) for k,v in dic.iteritems() if
>> v == update_form.cleaned_data['**branch_name']][0]) # Forcefully store
>> the key
>>         if update_form.is_valid():
>>             update_form.save()
>>
>> The form worked perfectly in admin but not from my own custom ModelForm.
>>
>> I don't know if my title is correct as I can see that Django saves the
>> selected element key in
>> the db and not its value which will be of type integer
>>
>> I'm getting the error from my oen custom ModelForm:
>> <ul class="errorlist"><li>branch_**name<ul class="errorlist"><li>Select a
>> valid choice.
>>  That choice is not one of the available choices.</li></ul></li></ul>
>>
>> Please how do I get the form saved with the selected branch value?
>>
>> Please help.
>>
>> Thanks
>>
>> --
>> Odeyemi 'Kayode O.
>> http://www.sinati.com. t: @charyorde
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Django users" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> django-users+unsubscribe@**googlegroups.com<django-users%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/**group/django-users?hl=en<http://groups.google.com/group/django-users?hl=en>
>> .
>>
>
> --
>
> Jani Tiainen
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to django-users+unsubscribe@**
> googlegroups.com <django-users%[email protected]>.
> For more options, visit this group at http://groups.google.com/**
> group/django-users?hl=en<http://groups.google.com/group/django-users?hl=en>
> .
>
>


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to