A simple database project

62 views
Skip to first unread message

Zeynel

unread,
Nov 7, 2009, 9:58:15 AM11/7/09
to Django users
Hello,

I am planning to build a demo prototype for a who-knows-who database
for the legal profession. Database will consist of lawyer name, school
and year graduated. Searching by lawyer name will return other lawyers
graduated from same law school the same year. Can I build this in
Django? How long would it take me to learn enough Django to build this
myself?

Thank you

Tomasz Zieliński

unread,
Nov 7, 2009, 12:23:46 PM11/7/09
to Django users
Of course you can build this in Django, I think that going through
tutorial:

http://docs.djangoproject.com/en/dev/intro/tutorial01/

is enough to build it in no longer that a few days (learning
included),
even for pre-intermediate programmer.

--
Tomasz Zieliński
http://pyconsultant.eu

Zeynel

unread,
Nov 7, 2009, 2:29:59 PM11/7/09
to Django users
Thanks. I started to study.

On Nov 7, 12:23 pm, Tomasz Zieliński

Zeynel

unread,
Nov 10, 2009, 7:21:00 PM11/10/09
to Django users
Hi Tomasz,

I was able to build the admin page and put a search box there:

class LawyerAdmin(admin.ModelAdmin):
fieldsets = [
('Name', {'fields': ['last', 'first', 'initial']}),
('School', {'fields': ['school', 'year_graduated']}),
]
list_display = ('first', 'initial', 'last', 'school',
'year_graduated')
list_filter = ['year_graduated']
search_fields = ['last', 'first']

This search box searches only first and last names. But what i want is
a search box that finds all lawyers who went to same school. My
database now contains:

Tom T. Lawyer3 Columbia School of Law 2003
Bob Lawyer2 NYU 2000
John I Lawyer1 NYU 2000

So Lawyer1 and Lawyer2 graduated from NYU in 2000.

So I want to enter in the search box "Lawyer1" and in the result page
I want to see "Lawyer1 knows Lawyer2"

How do I do this?

Thank you so much.

On Nov 7, 12:23 pm, Tomasz Zieliński
<tomasz.zielin...@pyconsultant.eu> wrote:

Kenneth Gonsalves

unread,
Nov 10, 2009, 7:35:28 PM11/10/09
to django...@googlegroups.com
On Wednesday 11 Nov 2009 5:51:00 am Zeynel wrote:
> So I want to enter in the search box "Lawyer1" and in the result page
> I want to see "Lawyer1 knows Lawyer2"
>
if your school is a foreign key (I think it is) you need a related name to
search for that. Check out ModelAdmin.search_fields in the docs. Not sure that
this is what you are looking for though.
--
regards
Kenneth Gonsalves
Senior Project Officer
NRC-FOSS
http://nrcfosshelpline.in/web/

Zeynel

unread,
Nov 11, 2009, 8:03:54 AM11/11/09
to Django users

In this document http://docs.djangoproject.com/en/dev/ref/contrib/admin/
ModelAdmin.search_fields is explained:

"You can also perform a related lookup on a ForeignKey with the lookup
API "follow" notation.

search_fields = ['foreign_key__related_fieldname']"

I tried

search_fields = ['school__lawyer']

but I get an error message when I did a search for last name of
lawyer:

Related Field has invalid lookup: icontains

When I try

search_fields = ['school__last']

I get the error message

Cannot resolve keyword 'last' into field. Choices are: id, lawyer,
school

Maybe I don't understand what "related_fieldname" is.

What is it?

This is my model:

class School(models.Model):
school = models.CharField(max_length=200)
def __unicode__(self):
return self.school


class Lawyer(models.Model):
first = models.CharField(max_length=20)
initial = models.CharField(blank=True, max_length=2)
last = models.CharField(max_length=20)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=200)
school = models.ForeignKey(School)
def __unicode__(self):
return self.first

Thank you

pjrh...@gmail.com

unread,
Nov 11, 2009, 1:01:32 PM11/11/09
to Django users
> I tried
>
> search_fields = ['school__lawyer']
>
> but I get an error message when I did a search for last name of
> lawyer:
>
> Related Field has invalid lookup: icontains

You need something like:

search_fields = ['school__lawyer_last']

to specify which field on the related 'lawyer' instance you want to
search. You can add others like:

search_fields = ['school__lawyer_last', 'school__lawyer_first']

Peter

Zeynel

unread,
Nov 11, 2009, 3:57:46 PM11/11/09
to Django users
Ok, I tried

search_fields = ['school__lawyer_first']

and I get this error

Cannot resolve keyword 'lawyer_first' into field. Choices are: id,
lawyer, school

In the variations that I tried only

search_fields = ['school__school']

worked.

I followed the tutorial and arranged lawyer names as a list, so that
when I search for school, I see which lawyers went to that school and
their year of graduation. Then I have the filter by year, and when I
apply that filter, I see all the lawyers who graduated from same
school same year. This is the result that I want, but obviously not a
very elegant solution.

Thank you.
Reply all
Reply to author
Forward
0 new messages