D
D
Dauren S2017-02-19 12:42:07
Django
Dauren S, 2017-02-19 12:42:07

Delete data from 3 table Many to many django?

class Category(models.Model):
    title = models.CharField(max_length=200)
    url = models.CharField(max_length=200,blank=True, null=True)
    parent = models.ForeignKey('Category',blank=True, null=True)
    description=models.CharField(max_length=200,blank=True, null=True)
    path=models.CharField(max_length=200,blank=True, null=True)
    metatitle=models.TextField(max_length=200,blank=True, null=True)
    metakey=models.TextField(max_length=200,blank=True, null=True)
    metadesc=models.TextField(max_length=200,blank=True, null=True)
   
    published=models.BooleanField(default=True)
    position=models.IntegerField(default=0)

class Article(models.Model):
    title=models.CharField(max_length=200)
    url = models.CharField(max_length=200,blank=True, null=True)
    description=RichTextUploadingField()
    fulltext=RichTextUploadingField(default='',blank=True, null=True)
    created=models.DateTimeField()
    image=models.ImageField(upload_to="media/",default='',blank=True, null=True)
    category=models.ManyToManyField(Category)
    position=models.IntegerField(default=0)
    hits=models.IntegerField(default=0)
    audio=models.BooleanField(default=False)
    audio_url=models.URLField(default='',blank=True, null=True)
    audio_file=models.FileField(upload_to='files/',default='',blank=True, null=True)
    video=models.BooleanField(default=False)
    video_code=models.CharField(default='',max_length=200,blank=True, null=True)
    video_url=models.URLField(default='',blank=True, null=True)
    user_id=models.IntegerField(default=0)
    metatitle=models.TextField(max_length=200,blank=True, null=True)
    metakey=models.TextField(max_length=200,blank=True, null=True)
    metadesc=models.TextField(max_length=200,blank=True, null=True)
    published=models.BooleanField(default=True)

Made the form now when updating, how can I delete the categories related to this entry?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dauren S, 2017-02-19
@dauren101

post = get_object_or_404(Article, pk=pk)
post.category.clear()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question