开发者

database design for user/reviewer in django database model

i am kinda new to database design, but i would like to create three tables "User" and "Review" and "Topic" for a database in django.

I will try to explain it in detail here:

For example, I have User, Topic and Review models in models.py. one user can only write one review for one topic from other users.

let's say: Mike, John, Peter are the three Users.

Mike posted "Hello World" topic. John can only write one review for the topic "Hello World", Peter can also write one review for the same. John and Peter can not post another review for the same topic(they can only modify it). If Mike post another topic, John and Peter can post another review for the new topic. the same rule apply to o开发者_开发问答ther users.

please if you could, could you please provide some sample code for this issue? thanks a lot.


If you are trying to figure out how to set up your models.py, visit the django documentation, and look at Writing your first app (https://docs.djangoproject.com/en/dev/intro/tutorial01/). It goes from start to finish writing your first application and you will learn how the system works.

If you wanted more specifics for the paradigm of your case, here's what I would do. I would probably handle this in the view/template and submit/edit the review with Dajaxice calls to the database. If a review by the current user exists, it will show the data, if it doesn't it will be a blank entry that will use Dajax to submit the content. In the python method that the Dajax calls, you would try to find a review, and if one exists while attempting to add a new one, something went wrong and you can handle the error, otherwise it is saved for all to see.

For example, in models.py:

class User(models.Model):
    name = models.CharField(max_length=128)
    def __unicode__(self):
        return self.name

class Review(models.Model):
    title = models.CharField(max_length=64)
    message = models.TextField()
    topic = models.ForeignKey(Topic)
    user = models.ForeignKey(User)
    def __unicode__(self):
        return self.title

class Topic
    title = models.CharField(max_length=64)
    message = models.TextField()
    user = models.ForeignKey()
    def __unicode__(self):
        return self.title

in views.py:

class Post(models.Model): # This is defined a model, but not part of the data layer, it really is view code.
    topic = None
    your_review = None
    other_reviews = None
    def __unicode__(self):
        return ''

def GetDetails(request):
    posts = () # to be returned to and looped by the Template.
    topics = Topic.objects.all().order_by('-posting_date') # posting_date descending.

    for t in topics:
        post = Post()
        post.topic = t
        post.your_review = Review.objects.filter(topic__id=t.id, user__id=<current_user_id>)
        post.other_reviews = Review.objects.filter(topic__id=t.id, ~Q(user__id=<current_user_id>)

        # Append to the posts array.
        posts.append(post)

    return render_to_response('index.htm', {'posts': posts}, context_instance=RequestContext(request))

in your index.htm:

{% if posts %}
    {% for p in posts %}
        <div>
            <div class="title">{{ p.topic.title }}</div>
            <div class="message">{{ p.topic.message }}</div>
            <div class="other_reviews">
                {% if p.other_reviews %}
                    {% for r in p.other_reviews %}
                        <div class="review_title">{{ r.title }}</div>
                        <div class="review_message">{{ r.message }}</div>
                    {% endfor %}
                {% endif %}

                <div>
                    <input type="text" value="{% if p.your_review %}{{ p.your_review.title }}{% endif %}">
                </div>
                <div>
                    <textarea>{% if p.your_review %}{{ p.your_review.message }}{% endif %}</textarea>
                </div>
            </div>
        </div>
    {% endfor %}
{% endif %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜