Optimisation Models

2011-10-26 Thread Suprnaturall
Optimisation Model

Hi everyone,

I'm working on the optimization of the model of my app. The goal is to
limit the request to the database.
In this webapp every user can follow users (like twitter), and when
someone you follow publish an article, each followers reveive a mail.
I'm wondering what is the best choice of models between this 2
example :

class UserProfile(models.Model):
...
followers = ManyToMany(self)

Or

class Followers(models.Model):
poster = OneToOne(UserProfile)
followers = ManyToMany(UserProfile)


First i was thinking about the example1, but I'm afraid that of each
time I will do a UserProfile.objects.get DJANGO join UserProfile table
to itself
in order to retrieve all the followers.

What do you think ?

Thx for your help :)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Problem with user.username

2011-07-12 Thread Suprnaturall
Hi,

First of all, apologize me for my poor, poor english.

I m new with django and i m stuck with a odd thing.

I created a login form and use the default django login handlet like
this :

base.html :
(...)   {% if user.is_authenticated %}
{{user.username }}
{% else %}
Login
{% endif %}
log outYour username and password didn't match. Please try again.
{% endif %}


{% csrf_token %}


{{ form.username.label_tag }}
{{ form.username }}


{{ form.password.label_tag }}
{{ form.password }}






{% endblock %}


The problem is : when i m in login.html i see my user.username, but
when i submit the login form and being redirect to index i don t
retrieve the user.username.
I add in my views.py this import :
from django.template import Context, loader, RequestContext
from empireFront.models import Video
from django.http import HttpResponse
from django.contrib import auth
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.contrib.sessions.models import Session

I heard something about use render_to_response but when, where and
how ?

Thx for your help !



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



group by and retrieve to template date from foreinKey

2011-07-15 Thread Suprnaturall
Hi dear django users :)

I m facing a stupid problem.
Here is my models.py :

class Video(models.Model):
(...)
category = models.ForeignKey('Category')
   (...)

class Category(models.Model):
name = models.CharField(max_length=250)

I'm trying in a tags to make a group by on the category field and
retrieve in the template the name of the first vid and its category
name.

Here it s my teamplate.py :

@register.inclusion_tag('appFront/home_tag.html')
def home_vods():
videos =
Video.objects.values('category').annotate(dcount=Count('category'))
return {'videos': videos}

And here it s the template.html


{% for video in videos %}
{{ video.title }}{{ video.title }}|
{{ video.category.name }}
{% endfor %}


I retrieve the correct number of row (in my case 3) but it s seems
that the query set is empty because it may invoque this query " SELECT
category FROM video group by category ", so i retrieve the correct
amount of row but the only field populate is category, on the other
hand i don t retrieve in my .html the date video.category.name, i got
nothing oO

Do you have any clue ? What is the correct way to make a group by and
then retrieve all the data in the table ?

Thx for your help :)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



custom method in models.py

2011-07-15 Thread Suprnaturall
Hi,

I'm wondering if like in play or symfony you can develop special
method for each class/table in the models.py and use them in your
template.
I'm trying few things but it didn't work.

For exemple this is my models.py :
class Category(models.Model):
name = models.CharField(max_length=250)

def get_short_name():
return name[0,20]

And here my html :
Desc : {{ category.get_short_name }} 

But it don t work, i'v got nothing in the screen.

What the correct way to do stuff like this, i m supposed to make a
custom tag in order to truncuate the name field ?

Thx

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Use object (User and custom object) in a form

2011-08-12 Thread Suprnaturall
Hello everyone,

I'm trying to add comment to a custom object (here Article).
This is my forms.py :

from django import forms
from django.contrib.auth.models import User
from myApp.models import Article

class CommentForms (forms.Form):
cf_comment = forms.CharField()
cf_writer = forms.User()
cf_date = forms.DateField()
cf_article = forms.article()

Here the template :







and here my view :

def article(request, id):
article = Article.objects.get(id=id)
commentaires =
Commentaire.objects.filter(article=article.id).order_by("-
dateComment")
# comment form
if request.method == 'POST':
form = CommentForms(request.POST)
if form.is_valid(): #validation
cf_comment = form.cleaned_data['commentaire']
cf_writer = form.cleaned_data['user']
cf_date = datetime.datetime.now()
cf_article = form.cleaned_data['article']
else :
form = CommentForms()

c = Context({
'Article' : article,
'commentaires' : commentaires,
})
return render_to_response('myApp/article.html', c,
context_instance=RequestContext(request))

When i try to go to /myapp/myArticle/id i have this message :
Tried article in module app.myApp.views. Error was: 'module' object
has no attribute 'User'

Do you have any idea ? maybe i forge an import or maybe i can t use
object in forms ...

Thx for your help !

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.