Hi,
It really depends what is the result you're after.
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.
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)
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.
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 django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
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 django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.