完整性错误,UNIQUE约束失败

时间:2015-08-05 18:19:03

标签: django python-2.7 django-views

我正在尝试允许用户上传照片并能够更改照片。每当用户回来并更改照片时,我都会收到完整性错误,“UNIQUE约束失败:portal_content.user_id”。我认为这是我试图在已经有用户时保存用户。但问题是,我不知道如何解决它并修复它。我已经看到了类似的问题,但是根据我的技能水平,我不确定如何解决它。这是我的代码:

型号:

def content_file_name(instance, filename):
    return '/'.join(['content', instance.user.username, filename])


class Content(models.Model):
    user = models.OneToOneField(User, unique=True)
    image1 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image2 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image3 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image4 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image5 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image6 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image7 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    image8 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    terms = models.ImageField(upload_to=content_file_name, null=True, blank=True)

查看:

@login_required
def register(request):
    if request.POST:
        content = Content()
        content.user = request.user
        content.image1 = request.FILES.get('image1_upload', None)
        content.image2 = request.FILES.get('image2_upload', None)
        content.image3 = request.FILES.get('image3_upload', None)
        content.image4 = request.FILES.get('image4_upload', None)
        content.image5 = request.FILES.get('image5_upload', None)
        content.image6 = request.FILES.get('image6_upload', None)
        content.image7 = request.FILES.get('image7_upload', None)
        content.image8 = request.FILES.get('image8_upload', None)
        content.terms = request.POST.get('terms')
        content.save()
        return redirect('/portal/register')
    try:
        gallery = Content.objects.get(user=request.user)
        return render(request, 'portal/register.html', {'gallery': gallery})
    except ObjectDoesNotExist:
        print 'Does Not Exist!'
        return render(request, 'portal/register.html')

模板:

{% extends 'portal/base.html' %}

{% load staticfiles %}
{% block head_block %}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script language="javascript" type="text/javascript" src="{% static 'js/input.js' %}"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" type="text/javascript"></script>

{% endblock %}
{% block body_block %}
<div class="container">
        <form role="form" method="post" action="." id="js-upload-form" enctype="multipart/form-data">
                            {% csrf_token %}
        <div class="row">

            <div class="col-lg-12">
                <h1 class="page-header">{{ user.username }}</h1>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image1" src="/media/{{ gallery.image1 }}" alt="">
                </a>
                <input type="file" name="image1_upload" id="image1_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image2" src="/media/{{ gallery.image2 }}" alt="">
                </a>
                <input type="file" name="image2_upload" id="image2_upload" multiple>
            </div>


            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image3" src="/media/{{ gallery.image3 }}" alt="">
                </a>
                <input type="file" name="image3_upload" id="image3_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image4" src="/media/{{ gallery.image4 }}" alt="">
                </a>
                <input type="file" name="image4_upload" id="image4_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image5" src="/media/{{ gallery.image5 }}" alt="">
                </a>
                <input type="file" name="image5_upload" id="image5_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image6" src="/media/{{ gallery.image6 }}" alt="">
                </a>
                <input type="file" name="image6_upload" id="image6_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image7" src="/media/{{ gallery.image7 }}" alt="">
                </a>
                <input type="file" name="image7_upload" id="image7_upload" multiple>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" id="image8" src="/media/{{ gallery.image8 }}" alt="">
                </a>
                <input type="file" name="image8_upload" id="image8_upload" multiple>
            </div>
        </div>
        <br>
             <!--<div class="form-group">-->
                 <!--<textarea name="terms" id="terms" class="form-control input-sm" placeholder="Terms" value="{{ content.terms }}"></textarea>-->
             <!--</div>-->
            <div class="form-group">
                <input type="text" name="terms" id="terms" class="form-control input-sm" placeholder="terms" value="{{ gallery.terms }}">
            </div>
        <br>
            <input type="submit" value="Register" id="js-upload-submit" class="btn btn-info btn-block">
        </form>
    <br>
    <!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  iPad View
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <div class="row">

            <div class="col-lg-12">
                <h1 class="page-header">{{ user.Username }}</h1>
            </div>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image1 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image2 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image3 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image4 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image5 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image6 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image7 }}" alt="">
                </a>
            </div>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <img class="img-responsive" src="/media/{{ gallery.image8 }}" alt="">
                </a>
            </div>
        </div>
          <p>
                {{ gallery.terms }}
            </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
    <script language="javascript" type="text/javascript" src="{% static 'js/preview.js' %}"></script>
{% endblock %}
<!--http://jsfiddle.net/Mqvgx/-->

1 个答案:

答案 0 :(得分:0)

您的register()视图每次都会尝试创建一个新的Content对象。由于每个用户只能与单个Content对象相关联,因此在尝试创建第二个用户时,它将失败。

您可以将content = Content()替换为content, created = Content.objects.get_or_create(user=request.user)。这将为用户处理多个Content对象。

P.S。更好的方法是使用Django's ModelForms