Delete a many to many field who is in another table

7 views
Skip to first unread message

jose osuna perez

unread,
Nov 30, 2011, 6:42:47 AM11/30/11
to Django users
Hi, I am finally going to finish the project started and where I end
up liking this xD Django that the problem I have is the clearing of a
field.
I have the following tables.

class Proyectos(models.Model):
titulo=models.CharField(max_length=100)
creacion=models.DateField(default=datetime.datetime.now)
estado=models.CharField(max_length=30)
objetivo=models.TextField(null=True)
conclusion=models.TextField(null=True)
porcentaje=models.IntegerField()
modificado=models.DateTimeField(default=datetime.datetime.now)
autor=models.IntegerField()
usuarios=models.ManyToManyField(User)
proyectos_rel=models.ManyToManyField("self")
documentos=models.ManyToManyField(Documentos)
class Meta:
db_table='Proyectos'
def __unicode__(self):
return self.titulo

class Experimentos(models.Model):
titulo=models.CharField(max_length=100)
creacion=models.DateField(default=datetime.datetime.now)
estado=models.CharField(max_length=30)
objetivo=models.TextField(null=True)
conclusion=models.TextField(null=True)
porcentaje=models.IntegerField()
modificado=models.DateTimeField(default=datetime.datetime.now)
autor=models.IntegerField()
proyecto=models.ForeignKey(Proyectos)
usuarios=models.ManyToManyField(User)
experimentos=models.ManyToManyField("self")
documentos=models.ManyToManyField(Documentos)
class Meta:
db_table='Experimentos'
.
I want to eliminate only one of the experiments which may contain a
Project
I try like this:

if request.POST.get('experimentosDelete','')!='':
for i in request.POST.getlist('experimentosDelete'):
exp=Experimentos.objects.get(proyecto=datos,id=i)
exp.experimentos.remove(request.POST.get('experimentosDelete'))

The result is that it does nothing ....... and otherwise remove whole
objects, not the relationship.I don't know .
I greatly appreciate your help in all questions.

Salvatore Iovene

unread,
Nov 30, 2011, 6:54:29 AM11/30/11
to django...@googlegroups.com
Hi,
after:

exp.experimentos.remove(request.POST.get('experimentosDelete'))

You should do:

exp.save()

I hope this helps.
Salvatore.

Tom Evans

unread,
Nov 30, 2011, 6:58:16 AM11/30/11
to django...@googlegroups.com
On Wed, Nov 30, 2011 at 11:42 AM, jose osuna perez
<jose.osu...@gmail.com> wrote:
> Hi, I am finally going to finish the project started and where I end
> up liking this xD Django that the problem I have is the clearing of a
> field.
> ...

> I try like this:
>
> if request.POST.get('experimentosDelete','')!='':
>                                                 for i in request.POST.getlist('experimentosDelete'):
>                                                        exp=Experimentos.objects.get(proyecto=datos,id=i)
>                                                        exp.experimentos.remove(request.POST.get('experimentosDelete'))
>

The remove() method takes objects, not ids.

https://docs.djangoproject.com/en/1.3/ref/models/relations/#django.db.models.fields.related.RelatedManager.remove

Cheers

Tom

jose osuna perez

unread,
Nov 30, 2011, 7:23:38 AM11/30/11
to Django users
I try to make this using delete, remove...... Also I using too

if request.POST.get('experimentosDelete','')!='':
for i in
request.POST.getlist('experimentosDelete'):

exp=Experimentos.objects.get(proyecto=datos,id=i)

exp.experimentos=[]

.......
I don't know how to make this......
Anyone who say me ??? Thanks a lot

Reply all
Reply to author
Forward
0 new messages