D
D
Dmitry Ivanov2018-03-15 06:18:42
Django
Dmitry Ivanov, 2018-03-15 06:18:42

Why is the image not saved?

Why is the image not saved?
I save through the model.
model

class Article(models.Model):
    title = models.CharField(max_length=230, default='0')
    url = models.SlugField(max_length=250, blank=True)
    description = models.TextField(default='0')
    edit = models.TextField(default='0')
    show = models.BooleanField(default=False)
    author = models.ForeignKey(User,  models.SET_NULL, blank=True, null=True)
    enc = models.ForeignKey(ListEnc,  models.SET_NULL, blank=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    create = models.DateTimeField(auto_now_add=True)

class ImageArticle(models.Model):
    image = models.ImageField(upload_to=get_file_path, blank=False, default='')
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    create = models.DateTimeField(auto_now_add=True)

def Img(request, views):
    if request.method == 'POST':
        img = ImageArticle(article_id= 4)
        img.image = request.FILES['0']
        img.save()

Outputs:
TypeError: __init__() got an unexpected keyword argument 'article_id
'
def Img(request, views):
    if request.method == 'POST':
        img = ImageArticle()
        img.image = request.FILES['0']
        img.save()

Outputs:
django.db.utils.IntegrityError: (1048, "Column 'article_id' cannot be null")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-03-15
@DmitrIvanov

Sometimes it seems to me that such questions are trolling!

img = ImageArticle()
img.article = Article.objects.get(pk=4)
img.image=request.FILES['0']
img.save()

If you really have a file field name in the form called '0'.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question