0

I am a beginner Django user and have been stuck with this problem for weeks now.

Consider the following model

class Post(models.Model):
    title = models.CharField(max_length=65)
    author = models.ForeignKey(User)
    date = models.DateField(auto_now_add=True)
    content = models.TextField()
    tags = models.ManyToManyField(Tag)
    images = models.ManyToManyField(Image)

def __unicode__(self):
    return self.title

I would like the User to be able to upload pictures to the with his model. I would like to be able to validate if

  1. The user has at least uploaded one image and limit the maximum number of images to a certain number.

  2. Basically do the same for tags. But with tags I would also like Django to check if the tags already exists and if so add it to the model.

  3. Let the user push a button that says add Tag / add Image to make a new field pop up.

Problems I have encountered so far.

  1. I am required to save a model before I can add many to many relations. This is a bit annoying because if an error occurs halfway down the road is might be possible that half the model is saved and the other is invalid.

  2. I know that I can add extra field to the DOM using js however I got no clue how to process those in the View function.

Niels
  • 1,513
  • 3
  • 20
  • 29

1 Answers1

1

For problem 1, this makes sense because if you create a model which is refered to by other models, said model key must exist. What I'd suggest you do is look into saving multiple models with a transaction.

2 is pretty easy, just use jQuery/Javascript to show/hide the appropriate fields in the browser based on the user's selection event.

Based on your comment, here's an example of how I handle data to and from the server

//Submit data to server, assuming you have already extracted out the relevant data values
$("some_button").click(function(e){
  $.ajax({
    url : "someUURLLocation/",
    type : "POST",
    data : {"data" : JSON.stringify({"field1" : var1, "field2" :var2}),
    dataType : "json",
    success : function(results){
      if (results.success == "true") {
        //handle DOM insertion and other stuff
      } else 
        alert(results.message);
    }
  });
}

urls.py:

from django.conf.urls import patterns, url
from $APPNAME import views

urlpatterns = patterns("",
  ...
  ...
  url(r'^someURLLocation/$', views.handleDataSubmission),
  ...
)

views.py:

from django.http import HttpResponse
from django.utils import simplejson

def handleDataSubmission(request):
  data = simplejson.loads(request.POST.get("data", None))

  if data is not None:

    newPost = Post.objects.create( field1 = data["field1"], field2 = data["field2"])
    dataReturn  = [{"val1" : newPost.title, "val2" : newPost.date}]

    return HttpResponse(simplejson.dumps({"success" : "true", "data" : dataReturn}), mimetype = "application/json")

  else:
    return HttpResponse(simplejson.dumps({"success" : "false", "message" : "Invalid data received by server"}), mimetype = "application/json")
Community
  • 1
  • 1
Jason
  • 11,263
  • 21
  • 87
  • 181
  • Like I said I know how how to add fields to the DOM just not how to process them in the view function. – Niels Aug 21 '13 at 13:23
  • Thanks for your example. I am just wondering can I also submit files using this approach? – Niels Aug 22 '13 at 13:39
  • Yes, you can handle file upload using this approach. However, I'd recommend you check out the jQuery FileUpload plugin and the information at http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example – Jason Aug 22 '13 at 14:00