Re: ForeignKey on CharField and char operations

53 views
Skip to first unread message

Tomas Neme

unread,
Jul 5, 2012, 9:40:00 AM7/5/12
to django...@googlegroups.com
I might not be understanding this fully, but what about
A.objects.filter(type__mnemo__startswith="type1")?

On Thu, Jul 5, 2012 at 7:51 AM, Serg Shtripling <ser...@shtripling.com> wrote:
> Hello, community!
> Have anyone tried this:
> class AType(models.Model):
> #cut
> mnemo = models.CharField(u'Mnemocode', max_length=31, null=True,
> unique=True)
>
> class A(models.Model):
> #cut
> type = models.ForeignKey(AType, 'mnemo', verbose_name=u'A Type',
> null=True)
>
> class B(models.Model):
> #cut
> type_mnemo = models.CharField(u'A Type', max_length=31, null=True,
> blank=True)
> Mnemos = {type1, type1__plus, type1__minus, type2, type2__plus,....}
> Now I want to get all A records, having AType starts with "type1", eg:
> "type1,type1__plus, type1__minus"
>
> r = A.objects.filter(type_id__startswith='type1')
> And got:
> TypeError: Related Field has invalid lookup: startswith
> As a workaround, I've replaced A model with B model - I do not have a
> relation, but query works as expected.
> Q: 1)Is it OK, that A.type_id, not just A.type field is a Related Field?
> q=AType.objects.all()[0]
>>>> type(q.type)
> <class 'main.models.AType'>
>>>> type(q.type_id)
> <type 'unicode'>
> 2) Is there a more correct way to solve this issue?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/aV3iT_l4v1kJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Serg Shtripling

unread,
Jul 5, 2012, 1:02:42 PM7/5/12
to django...@googlegroups.com


четверг, 5 июля 2012 г., 20:40:00 UTC+7 пользователь Tomas Neme написал:
I might not be understanding this fully, but what about
A.objects.filter(type__mnemo__startswith="type1")?

Wouldn't it create INNER JOIN query on a_type table? AFAIR it would and it's a bit overhead.
But surely it is a better solution than mine one, thanks for that.

Tomas Neme

unread,
Jul 5, 2012, 1:19:54 PM7/5/12
to django...@googlegroups.com
> Wouldn't it create INNER JOIN query on a_type table? AFAIR it would and it's
> a bit overhead.
> But surely it is a better solution than mine one, thanks for that.

Well, in that case, I think your problem is that your AType PK isn't
the mnemo field, but some integer field, so type_id is integer, not
string.

Try defining it as

mnemo = models.CharField(u'Mnemocode', max_length=31, null=True,
unique=True, primary_key=True)

but I'm not sure if this is the behavior you want
Reply all
Reply to author
Forward
0 new messages