Re: need help with model design and table relationship

2009-08-10 Thread Unnamed_Hero

Here is the solution. There is no need in Many-to-many here.
In people table I've set corp's unique number as a ForeignKey to
Corp's table.
In a template:
  {% for i in result %}
   {% for persons in i.dolgnost_set.all %}
{{ person.field1 }}


where Dolgnost - a table with people.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-05 Thread Malcolm Tredinnick

On Tue, 2009-08-04 at 14:40 -0700, Unnamed_Hero wrote:
> 
> > And you still haven't reduced it to a simple case that fails with a
> > specific piece of data at the interactive prompt. You are trying to
> > debug through three layers of curtains here. Why not remove the extra
> > layers and work directly with the ORM to debug an ORM problem, as I
> > suggested in the first piece of email?
> 
> I have tried to work in interactive shell, re-read django docs, but
> still -
> relation "C12B_boss" does not exist. (I'm using postres, and it is
> error not from django, but from DB)

That's unsurprising. The problem isn't going to suddenly vanish! It's
going to be easier to debug because you'll be able to find a single
piece of data that demonstrates the problem. Then you will be able to
construct a smaller example that shows the problem so that we can try it
out for ourselves.

Also, as I've suggested previously, look at the SQL schema Django is
generating. Compare that to what you're trying to match in the existing
database and work on making the differences go away.

There is nothing more I can do to help you here. You need to reduce
things to a much simpler example to isolate the problem. This is
fundamental debugging technique and there is no avoiding it. Only you
have access to the system that demonstrates the problem.

Regards,
Malcolm


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-04 Thread Unnamed_Hero


> And you still haven't reduced it to a simple case that fails with a
> specific piece of data at the interactive prompt. You are trying to
> debug through three layers of curtains here. Why not remove the extra
> layers and work directly with the ORM to debug an ORM problem, as I
> suggested in the first piece of email?

I have tried to work in interactive shell, re-read django docs, but
still -
relation "C12B_boss" does not exist. (I'm using postres, and it is
error not from django, but from DB)

It seems to me solution is simple, but I can't find it.




--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-03 Thread Malcolm Tredinnick

On Mon, 2009-08-03 at 08:27 -0700, Unnamed_Hero wrote:
[...]
> And I still get
> Caught an exception while rendering:relation "C12B_boss" doesn't
> exist


And you still haven't reduced it to a simple case that fails with a
specific piece of data at the interactive prompt. You are trying to
debug through three layers of curtains here. Why not remove the extra
layers and work directly with the ORM to debug an ORM problem, as I
suggested in the first piece of email?

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-03 Thread Unnamed_Hero

And again for your explanations. So I have changed my models as you
advise.
Also I've made a form from c12b model, and when it displays - I see
all peoples from table with people.
But I still can't fetch any results 
Maybe by queryset isn't good.
manage.py sql  return this:
people table:
CREATE TABLE "Dolgnost" (
"A22" varchar(8) NOT NULL PRIMARY KEY,
"U1" varchar(250) NOT NULL,
"U2" varchar(50) NOT NULL,
"U3" varchar(50) NOT NULL,
"U4" varchar(2) NOT NULL
)
;

corp table
CREATE TABLE "C12B" (
"A1" varchar(250) NOT NULL,
"A2" varchar(50) NOT NULL,

"A22" varchar(8) NOT NULL PRIMARY KEY,
"A40" varchar(3) NOT NULL,


#It seems it is a relation table by django
CREATE TABLE "C12B_boss" (
"id" serial NOT NULL PRIMARY KEY,
"c12b_id" varchar(8) NOT NULL REFERENCES "C12B" ("A22") DEFERRABLE
INITIALLY DEFERRED,
"dolgnost_id" varchar(8) NOT NULL REFERENCES "Dolgnost" ("A22")
DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("c12b_id", "dolgnost_id")
)
;

in models
in Dolgnost model
A22  = models.CharField ('Код ОКПО', max_length=8,  blank=True,
unique=False, primary_key=True)

in c12b model
A22 = models.CharField ('Код ОКПО', max_length=8, blank=True,
primary_key=True)
boss = models.ManyToManyField (Dolgnost)

And I still get
Caught an exception while rendering:relation "C12B_boss" doesn't
exist

Still can't realize what I need to do.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-03 Thread Malcolm Tredinnick

On Mon, 2009-08-03 at 03:19 -0700, Unnamed_Hero wrote:
> Thanks for your reply.
> 
> And the point is that (forgot to say)... the boss field in c12b table
> doesn't exists, it is virtual and exists only in my model.

When you add a ManyToManyField to a Django model, it doesn't create a
column in that model's database table, so that shouldn't be a problem.
However, db_column doesn't make sense on a ManyToMany field, for
precisely that reason. Either you use a "through" table to specify the
columns via the intermediate table, or it will refer to the primary key
field on the model.

>  I think,
> this trick can work for me, but I've got (c12b_boss relation does not
> exist). I thought, that if I point it to a real A22 field (unique code
> field) through "db_column" directive, because there are several tables
> with such relations...; but I failed. And I still can't clearly
> understand what odel design I need.

One way to try to work out whether your model is close is to look at the
output of "manage.py sql " and compare that to the SQL you are
trying to achieve. Then you can at least work out what fields need to be
changed.

> PS. Such field's names, data structure - are not mine work. this
> database was designed over 15 years ago, and maintained by other
> people...

The name of the database columns does not prevent you from having
descriptive names for the model fields. It's well worth the time, for
your own sanity, and ours, to do so. Use the db_column parameter on each
model field to then set the database column name correctly. That is a
nice feature of Django's design: you can still give the Python-level
attributes reasonably understandable names, no matter what sort of
database naming scheme was in place.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-03 Thread Unnamed_Hero

Thanks for your reply.

And the point is that (forgot to say)... the boss field in c12b table
doesn't exists, it is virtual and exists only in my model. I think,
this trick can work for me, but I've got (c12b_boss relation does not
exist). I thought, that if I point it to a real A22 field (unique code
field) through "db_column" directive, because there are several tables
with such relations...; but I failed. And I still can't clearly
understand what odel design I need.

PS. Such field's names, data structure - are not mine work. this
database was designed over 15 years ago, and maintained by other
people...
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: need help with model design and table relationship

2009-08-02 Thread Malcolm Tredinnick

On Sun, 2009-08-02 at 14:08 -0700, Unnamed_Hero wrote:
> I have a table with a list of corporations; each corp has a field with
> it unique number.
> There is a table, filled with people, each record contains a field
> with a corp's unique field (where each person works)
> 
> Task: when displaying info about corporation, also fetch info about
> people working at.
> 
> Also there are several tables with such relationship with a corp
> table.
> 
> So, it seems to be a many-to-many relationship.
> 
> there is come code
> 
> class c12b (models.Model):
> id = models.AutoField (primary_key=True)
> A15 = models.CharField ('Телетайп', max_length=20,  blank= True)
> A17 = models.CharField ('Телекс', max_length=20,  blank= True)
> A19 = models.CharField ('Факс', max_length=40,  blank= True)
> A34  = models.CharField ('Код промышленности',max_length=5,
> blank=True)
> A29 = models.CharField ('E-mail', max_length=50, blank=True)
> A30 = models.CharField ('Сайт', max_length=40,  blank= True)
> A3 = models.CharField ( 'ИНН',max_length=10, blank=True)
> A21  = models.CharField ('Реквизиты банка' ,max_length=255,
> blank=True)
> A22 = models.CharField ('Код ОКПО', max_length=8, blank=True )
> #CORP INIQUE KEY
> #here I've tried to create a related field
> boss = models.ManyToManyField (Dolgnost, db_column="A22",
> through='bosses_table')
> class Meta:
> db_table='C12B'
> 
> class bosses_table (models.Model):
> c12b = models.ForeignKey (c12b,  to_field="A22")

I'm surprised Django didn't raise a validation error here, since
c12b.A22 is required to be a unique field in order to be the target of a
to_field parameter and you haven't marked it as unique in the above
model.

> dolgnost = models.ForeignKey (Dolgnost,
> to_field="A22",unique=False)
> 
> 
> #in view.py
> ...
> result = c12b.objects.filter(A34__exact ="02100")
> return render_to_response ("prkp/lista.html",  {
>"result":result,
> ...
> #template
> {% for i in result %}
> 
> {% for b in i.boss.all %}
> {{ b.U1 }}
> {% endfor %}
> {{ i.boss.U1 }}
> 
> Finally I've got an exeption
> Caught an exception while rendering:relation "search_bosses_table"
> doesn't exist

I would start by removing as many variables from the debugging situation
as possible. In particular, instead of debugging this through the
template system, use an interactive shell. Work out the exist record in
"result" that is causing the problem so that you can narrow things down
to the exact piece(s) of data that are causing the problem.

There doesn't seem to be anything immediately wrong with your models,
although I would put some effort into naming the fields more
descriptively ("A34" is hardly easy to debug when it pops up in a
traceback). I would also simply make A22 the primary key of the c12b
model (and probably the boss model as well, if it's similar) so that you
don't even need the intermediate table and can just use a normal
ManyToManyField. Generally, if you are using "through" on a
ManyToManyField, it would be because the intermediate table contains
more than just the two linking fields.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---