django数据库模型中用户/审阅者的数据库设计

时间:2022-08-25 12:56:24

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.

我是数据库设计的新手,但我想为django中的数据库创建三个表“User”和“Review”和“Topic”。

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.

例如,我在models.py中有User,Topic和Review模型。一个用户只能为其他用户的一个主题撰写一篇评论。

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 other users.

迈克发布了“Hello World”主题。 John只能为“Hello World”主题撰写一篇评论,Peter也可以为此撰写一篇评论。约翰和彼得不能为同一主题发布另一篇评论(他们只能修改它)。如果迈克发表另一个主题,约翰和彼得可以发布另一个新主题的评论。同样的规则适用于其他用户。

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

如果可以的话,请您提供一些此问题的示例代码?非常感谢。

1 个解决方案

#1


1  

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.

如果您想弄清楚如何设置models.py,请访问django文档,然后查看编写您的第一个应用程序(https://docs.djangoproject.com/en/dev/intro/tutorial01/)。它从头到尾编写您的第一个应用程序,您将了解系统的工作原理。

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.

如果你想要更具体的案例范例,这就是我要做的。我可能会在视图/模板中处理这个问题,并使用Dajaxice调用数据库来提交/编辑评论。如果当前用户的评论存在,它将显示数据,如果不存在,它将是一个空白条目,将使用Dajax提交内容。在Dajax调用的python方法中,您将尝试查找评论,如果在尝试添加新评论时存在,则出现错误并且您可以处理错误,否则将保存以供所有人查看。

For example, in models.py:

例如,在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:

在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 %}

#1


1  

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.

如果您想弄清楚如何设置models.py,请访问django文档,然后查看编写您的第一个应用程序(https://docs.djangoproject.com/en/dev/intro/tutorial01/)。它从头到尾编写您的第一个应用程序,您将了解系统的工作原理。

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.

如果你想要更具体的案例范例,这就是我要做的。我可能会在视图/模板中处理这个问题,并使用Dajaxice调用数据库来提交/编辑评论。如果当前用户的评论存在,它将显示数据,如果不存在,它将是一个空白条目,将使用Dajax提交内容。在Dajax调用的python方法中,您将尝试查找评论,如果在尝试添加新评论时存在,则出现错误并且您可以处理错误,否则将保存以供所有人查看。

For example, in models.py:

例如,在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:

在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 %}