Search code examples
djangodjango-formsdjango-class-based-views

Django UpdateView creates a new object instead of updating the current object


Everytime I use UpdateView and try to change some attributes it creates a new object in my database by using a new unique Primary Key. My model is as below.

class Delivery(models.Model):
    created_date = models.DateTimeField('date created', editable=False)
    modified_date = models.DateTimeField('modified', editable=False)
    user_name = models.ForeignKey(User, null=False)
    stream_name = models.CharField(max_length=50, null=False)
    view_name = models.CharField(max_length=100, null=False, blank=True)
    activity_name = models.CharField(max_length=100, null=False, blank=True)
    jira = models.URLField()
    codereview = models.URLField()
    related_streams = models.CharField(max_length = 100, choices=sorted(streams()))
    description = models.TextField(null=False,blank=True)
    status = models.BooleanField(default=False, blank=False)

    class Meta:
        verbose_name = "Delivery"
        verbose_name_plural = "Deliveries"
        unique_together = (("user_name", "view_name", "activity_name"),)

For the form I am using the ModelFactoryForm that UpdateView uses by default to pick out a form_class using the model itself.

class UpdateEntryView(UpdateView):
    template_name = 'tracker/update.html'
    model = Delivery
    success_url = reverse_lazy('table_view')
    status = StreamStatus()
    fields = ['stream_name','view_name','activity_name','jira','related_streams','description','status']

    def get_context_data(self, **kwargs):
        ctx = super(UpdateEntryView, self).get_context_data(**kwargs)
        ctx['locked'] = self.status.getLocked()
        ctx['unlocked'] = self.status.getUnlocked()
        return ctx

The HTML template is as below :

...
{% block content %}
<form action="/tracker/entry/" method="post" class="form">
    {% csrf_token %}
    <div class="form-goup">
        <div class="panel panel-default">
            <div class="panel-heading">Enter the codereview link and let the machine do the work for you ...</div>
            <div class="panel-body">
                <div class="input-group">
                    <i class="glyphicon glyphicon-link input-group-addon" aria-hidden="true"></i>
                    <input type="text" class="form-control" name="codereview"  placeholder="Codereview link">
                </div>
            </div>
        </div>
        <hr>
        <div class="panel panel-default">
            <div class="panel-heading">Add an entry manually ...</div>
            <div class="panel-body">
                {% bootstrap_form form %}
            </div>
        </div>
        <hr>
        {% buttons %}
        <button type="submit" class="btn btn-primary btn-block">
        {% bootstrap_icon "lock" %} Submit
        </button>
        {% endbuttons %}
    </div>
</form>
{% endblock %}
...

Is this a flaw in UpdateView or is it something on my part which is screwing this up.


Solution

  • Is it possible your url is pointing to the CreateView instead of the UpdateView?