On 6 Gen, 00:41, drakkan <drakkan1...@gmail.com> wrote:
> On 6 Gen, 00:32, Malcolm Tredinnick <malc...@pointy-stick.com> wrote:
>
>
>
> > On Mon, 2009-01-05 at 06:31 -0800, drakkan wrote:
> > > however if i delete an user with records associated in the view I have
> > > this error:
>
> > > NotSupportedError: cannot delete from a view
> > > HINT: You need an unconditional ON DELETE DO INSTEAD rule.
>
> > > django try to delete record from the view and this obviously fails,
>
> > > there is a know workaround for this? any way to declare read only the
> > > model?
>
> > There's no way to say a model is read only. But the solution is easy:
> > don't try to delete anything on it. You are in complete control of your
> > code. It's entirely up to you whether you try to delete or save things
> > on a model, so if it hurts when you do that, just don't do it.
>
> Malcolm, I try to delete an user from the auth_user table; the user is
> referenced as foreign key in the view and the delete cascade django
> default try to delete all the reference,
>
> I need a foreign key like in the view to write code such as:
>
> databaseview.user.username
>
> so I can use select_related and get all needed database object with
> only one query (I use the database view to aggregate some many to many
> tables)
>
> so a possible solution is delete the referenced foreign key from the
> table on which the views is based on and then delete the target object
> (an user object in my example),
>
> however this is error prone, I'm searching a more clean solution such
> as redefine delete() or some hack with custom manager,
>
> thanks
> drakkan
>
>
Ok, I'll try to explain with an example, with user objects I mean
standard django users (django.contrib.auth)
table1:
class table1(models.Model):
field1=....
field2=...
user=models.ForeignKey(User)
I have a database view with fileds from table1 and some others
database tables:
class databaseview(models.Model):
field1table1=....
field1table2=...
....
user=models.ForeignKey(User)
I defined foreignkey relation in the models mapped to database view
too. This works fine when I access related objects and with
select_related I can load a full graph of object with only one
database query.
Suppose I want to delete an user. This user is in table1 and so in
databaseview too. Django default is to do a delete cascade so it tries
to remove the user from table1, databaseview and auth_user: it fails
when try to remove user from databaseview.
A possible solution would be :
- before issue user.delete(), issue something like
table1.objects.filter(user=u).delete() (this way user reference
disappear from databaseview too) and then u.delete()
however in my application I have several foreign key defined in the
model for the database view so this solution can be error prone, would
be really useful a way to define my mapped view read only or override
the delete cascade for this table,
is this possible with django or I need to find some other solution?
thanks
drakkan
>
> > 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
-~----------~----~----~----~------~----~------~--~---