Django signals created kwargs is True while updating

2018-07-24 Thread Aamu Padi
I think I am missing something here, but here it goes. I have a model, and
it is a sender for a post_save signal.

class Book(models.Model):
...
created_at = models.DateTimeField(default=timezone.now)
bought = models.BooleanField(default=False)

@receiver(post_save, sender=Book)
def create_sold_log(sender, instance, **kwargs):
if kwargs['created'] and instance.bought:
print('created and instance.bought')
print('k* created', kwargs['created'])

elif kwargs['created']:
print('created only')
print('k* created', kwargs['created'])
else:
print('else...')


Now everytime a book object is saved, the above signal will fire.

So from the below data, after the serializer is saved while the Bought is
True, the kwargs['created'] is True.

data = {
"...": "...",
"bought": True
}

serializer = CustomSerializer(data=data)
if serializer.is_valid():
serializer.save()

And suppose, from the below data, if the bought=False and I save the
serializer, then also kwargs['created'] is True.

data = {
"...": "...",
"bought": False
}

serializer = CustomSerializer(data=data)
if serializer.is_valid():
serializer.save()

But, suppose, later if I update the book object to Bought=True and save it,
the kwargs['created'] is still True.

book.bought = True
book.save()

What am I missing here? Can someone please help me understand. Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtw1HYP6jz1sNgin%2BDHPrKxH-v7xqzq8mUobSk%2BAQ1_ag%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Group by object's field and return list of object or object's id for each group

2018-05-07 Thread Aamu Padi
 class Ticket(models.Model):
...
booked_at = models.DateTimeField(default=timezone.now)
bought = models.BooleanField(default=False)

I would like to group tickets by booked day to get list of ticket or
ticket's id for each day. Something like this:

[
{
'day': datetime.datetime(2018, 5, 6, 0, 0, ...>),
'ticket_list': [1, 2, 3, 4],
},
{
'day': datetime.datetime(2018, 5, 7, 0, 0, ...>),
'ticket_list': [5, 6, 7, 8, 9],
}
]

I could group tickets by day this way and count total tickets per day,

Ticket.objects.filter(bought=True).annotate(day=TruncDay('booked_at')).values('day').annotate(c=Count('id')).order_by()

But I cannot figure out how to group by day and return ticket objects for
that day. Could you please help me solve this.

Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsa_7RtWzopQdJa7NK_vzZtshMxMuPoc1Vsz9HiWu_GVA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Django @transaction.atomic() to prevent creating objects in concurrency

2018-04-20 Thread Aamu Padi
I have a ticket model, and its ticket serialiazer. The ticket model has a
*bought* and a *booked_at* field. And also a *unique_together* attribute
for *show* and *seat*.

class Ticket(models.Model):
show = models.ForeignKey(Show, on_delete=models.CASCADE)
seat = models.ForeignKey(Seat, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
booked_at = models.DateTimeField(default=timezone.now)
bought = models.BooleanField(default=False)

class Meta:
unique_together = ('show', 'seat')


   - On the ticket serializer, the serializer on validation checks whether
   the ticket was bought or not.
   - If it is bought, then it will raise an error.
  - If its not bought then Check if the ticket was booked within 5
  minutes.
 - If its booked within 5 minutes, then raise an error.
 - Else if the booked time is more than 5 minutes, than delete the
 old ticket and return valid.

TicketSerializer:

class TicketSerializer(serializers.Serializer):
seat = serializers.PrimaryKeyRelatedField(queryset=Seat.objects.all())
show = serializers.PrimaryKeyRelatedField(queryset=Show.objects.all())
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
bought = serializers.BooleanField(default=False)

def validate(self, attrs):
if attrs['seat']:
try:
ticket = Ticket.objects.get(show=attrs['show'], seat=seat)
if not ticket.bought:
if ticket.booked_at < timezone.now() -
datetime.timedelta(minutes=5):
# ticket booked crossed the deadline
ticket.delete()
return attrs
else:
# ticket in 5 mins range
raise serializers.ValidationError("Ticket with same
show and seat exists.")
else:
raise serializers.ValidationError("Ticket with same
show and seat exists.")
except Ticket.DoesNotExist:
return attrs
else:
raise serializers.ValidationError("No seat value provided.")


On the view, I am using *@transaction.atomic()* to make sure the ticket/s
are created only if all of them are valid, or don't create ANY ticket if
not valid.

@transaction.atomic()
@list_route(
methods=['POST'],
)
def book_tickets_by_show(self, request, show_id=None):
try:
show = Show.objects.get(id=show_id)
user = request.user
...
...
data_list = [...]
with transaction.atomic():
try:
serializer = TicketSerializer(data=data_list, many=True)
if serializer.is_valid():
serializer.save()

return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
except (Seat.DoesNotExist, ValueError, ConnectionError) as e:
return Response({'detail': str(e)},
status=status.HTTP_400_BAD_REQUEST)
except (Show.DoesNotExist, IntegrityError) as e:
return Response({'detail': str(e)},
status=status.HTTP_400_BAD_REQUEST)

What I would like to know is will it help in preventing when more than one
requests are called for creating the ticket/s for same seat/s?

Suppose, User A wants to book ticket with seat 5,6 and User B wants to book
ticket with seat 3,6, and another User C wants to book the ticket with
seats 2,3,4,5,6. Will the above method prevent booking tickets for their
respective seats for all the users, and only create tickets for one user
(maybe whose transaction was first)? Or if there is a better way then could
you please advice me how to. I hope I was clear. If not please ask.

Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsHM%3Dk1ezHr3Vp_k9_4fviz%2B7ZmzZ7iuieZgifLg09k2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to perform grouping and ordering

2017-04-23 Thread Aamu Padi
I have a Ticket booking model

class Movie(models.Model):
   name = models.CharField(max_length=254, unique=True)

class Show(models.Model):
   day = models.ForeignKey(Day)
   time = models.TimeField(choices=CHOICE_TIME)
   movie = models.ForeignKey(Movie)

class MovieTicket(models.Model):
   show = models.ForeignKey(Show)
   user = models.ForeignKey(User)
   purchased_at = models.DateTimeField(default=timezone.now)

I would like to filter MovieTicket with its user field and group them
according to its show field, and order them by the booked_at field. And
respond back with json data using Django rest framework like this:

[
   {
   show: 4,
   movie: "Lion king",
   time: "07:00 pm",
   day: "23 Apr 2017",
   total_tickets = 2
   },
   {
   show: 7,
   movie: "Gone girl",
   time: "02:30 pm",
   day: "23 Apr 2017",
   total_tickets = 1
   }
]

I tried this way:

>>>
MovieTicket.objects.filter(user=23).order_by('-booked_at').values('show').annotate(total_tickets=Count('show'))


As you can see its not grouping according to the show. Also how can I add
other related fields (i.e., show__movie__name, show__day__date, show__time)?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWs_6jmDsMGnJ79O21bhkKhpwq0NLjgGtLUK2njdC%2BfYHw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Creating multiple objects with one request in Django Rest Framework

2017-04-18 Thread Aamu Padi
@Camilo Hi, I tried this way

class ManyMovieTicketSerializer(serializers.ModelSerializer):

seats = serializers.ListField(

child=serializers.IntegerField(min_value=0, max_value=100)

)

class Meta:

model = MovieTicket

fields = '__all__'


But I am getting error:

*AttributeError at /api/movies/buy-many-tickets/*


Got AttributeError when attempting to get a value for field `seats` on
serializer `ManyMovieTicketSerializer`.
The serializer field might be named incorrectly and not match any attribute
or key on the `MovieTicket` instance.
Original exception text was: 'MovieTicket' object has no attribute 'seats'.

What am I missing?

On Tue, Apr 18, 2017 at 4:45 PM, Christian Ledermann <
christian.lederm...@gmail.com> wrote:

> maybe
> https://github.com/marcgibbons/django-rest-framework-bulk
>
> HTH
>
> On 18 April 2017 at 11:29, Camilo Torres <camilotorr...@gmail.com> wrote:
> > Hi,
> >
> > May be you can create a new Serializer, including a ListField to get
> > multiple seat IDs:
> >
> > class ManyMovieTicketsSerializer(serializers.Serializer):
> > show = serializers.IntegerField
> > user = serializers.IntegerField
> > seats = serializers.ListField(child=serializers.IntegerFields)
> >
> > def create(self, validated_data):
> >     iterate over seats and save every instance.
> >
> > On Monday, April 17, 2017 at 1:19:23 AM UTC-4, Aamu Padi wrote:
> >>
> >> I am using Django as the backend server and Vue.js for the front end
> Movie
> >> app.
> >>
> >> I have a Ticket model
> >>
> >> class MovieTicket(models.Model):
> >> show = models.ForeignKey(Show)
> >> seat = models.ForeignKey(Seat)
> >> user = models.ForeignKey(User)
> >> purchased_at = models.DateTimeField(default=timezone.now)
> >> qrcode = models.ImageField(upload_to='qrcode', blank=True,
> null=True)
> >> qrcode_data = models.CharField(max_length=255, unique=True,
> >> blank=True)
> >>
> >> class Meta:
> >> unique_together = ('show', 'seat')
> >>
> >> And its related Serializer
> >>
> >> class MovieTicketSerializer(serializers.ModelSerializer):
> >> class Meta:
> >> model = MovieTicket
> >> fields = '__all__'
> >>
> >> To buy a new Ticket there's a view which is mapped to this url
> >> http://dev.site.com/api/movies/buy-ticket/:
> >>
> >> @api_view(['POST'])
> >> @permission_classes([IsAuthenticated])
> >> def buy_ticket(request):
> >> serialized = MovieTicketSerializer(data=request.data)
> >> if serialized.is_valid():
> >> serialized.save()
> >> return Response(serialized.data, status=status.HTTP_201_
> CREATED)
> >> return Response(serialized._errors,
> >> status=status.HTTP_400_BAD_REQUEST)
> >>
> >> Now from the front end (Vue.js) I can create a new movie ticket like
> this:
> >>
> >> const formBody = {
> >> show: this.$store.state.showSelected.showTime.id,
> >> user: this.$store.state.user.id,
> >>
> >> // selectedSeats is an array of seats that have been selected by the
> >> user. Here I am passing the first seat object.
> >> seat: this.$store.state.selectedSeats[0].seat.id
> >> };
> >>
> >> this.$http.post("http://dev.site.com/api/movies/buy-ticket/;, formBody)
> >> .then(function (response) {
> >> console.log(response.data);
> >> })
> >> .catch(function (response) {
> >> console.log(response);
> >> });
> >> return;
> >>
> >> If the form was valid, this will create a new MovieTicket Object.
> >>
> >> Now, suppose if the user selected multiple seats, I can loop through
> each
> >> selectedSeats array and get the seat ids. But what I am confused is how
> can
> >> I pass multiple seat.id if Django rest framework is only accepting one
> seat
> >> per request?
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to django-users+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-users@googlegroups.com.
> > Visit this group at https://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> 

Re: Creating multiple objects with one request in Django Rest Framework

2017-04-17 Thread Aamu Padi
Hi,

I couldn't find anything useful in that link that can help me solve my
problem. What am I missing?

On Mon, Apr 17, 2017 at 10:54 AM, m712 - Developer <
comeon@getbackinthe.kitchen> wrote:

> Hi,
> Take a look at https://docs.djangoproject.com/en/1.11/topics/db/
> examples/many_to_many/
> On Apr 17, 2017 8:19 AM, Aamu Padi <aamup...@gmail.com> wrote:
>
> I am using Django as the backend server and Vue.js for the front end Movie
> app.
>
> I have a Ticket model
>
> class MovieTicket(models.Model):
> show = models.ForeignKey(Show)
> seat = models.ForeignKey(Seat)
> user = models.ForeignKey(User)
> purchased_at = models.DateTimeField(default=timezone.now)
> qrcode = models.ImageField(upload_to='qrcode', blank=True, null=True)
> qrcode_data = models.CharField(max_length=255, unique=True, blank=True)
>
> class Meta:
> unique_together = ('show', 'seat')
>
> And its related Serializer
>
> class MovieTicketSerializer(serializers.ModelSerializer):
> class Meta:
> model = MovieTicket
> fields = '__all__'
>
> To buy a new Ticket there's a view which is mapped to this url 
> *http://dev.site.com/api/movies/buy-ticket/
> <http://dev.site.com/api/movies/buy-ticket/>:*
>
> @api_view(['POST'])@permission_classes([IsAuthenticated])def 
> buy_ticket(request):
> serialized = MovieTicketSerializer(data=request.data)
> if serialized.is_valid():
> serialized.save()
> return Response(serialized.data, status=status.HTTP_201_CREATED)
> return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
>
> Now from the front end (Vue.js) I can create a new movie ticket like this:
>
> const formBody = {
> show: this.$store.state.showSelected.showTime.id,
> user: this.$store.state.user.id,
>
> // selectedSeats is an array of seats that have been selected by the 
> user. Here I am passing the first seat object.
> seat: this.$store.state.selectedSeats[0].seat.id};
> this.$http.post("http://dev.site.com/api/movies/buy-ticket/;, formBody)
> .then(function (response) {
> console.log(response.data);
> })
> .catch(function (response) {
> console.log(response);
> });return;
>
> If the form was valid, this will create a new MovieTicket Object.
>
> Now, suppose if the user selected multiple seats, I can loop through each
> selectedSeats array and get the seat ids. But what I am confused is how
> can I pass multiple seat.id if Django rest framework is only accepting
> one seat per request?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/CAHSNPWv7yXiCM2VAHU3MBb19nLADU
> 49UH5ZONKW5C7YEUTtfmg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAHSNPWv7yXiCM2VAHU3MBb19nLADU49UH5ZONKW5C7YEUTtfmg%40mail.gmail.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/58f451a8.4d881c0a.8d9c5.038cSMTPIN_
> ADDED_MISSING%40gmr-mx.google.com
> <https://groups.google.com/d/msgid/django-users/58f451a8.4d881c0a.8d9c5.038cSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuVqivpyV%2BqsKe8Ju_J5qZjCrmBSVVpGd%3Dw%3D7fxXQ1wLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Creating multiple objects with one request in Django Rest Framework

2017-04-16 Thread Aamu Padi
I am using Django as the backend server and Vue.js for the front end Movie
app.

I have a Ticket model

class MovieTicket(models.Model):
show = models.ForeignKey(Show)
seat = models.ForeignKey(Seat)
user = models.ForeignKey(User)
purchased_at = models.DateTimeField(default=timezone.now)
qrcode = models.ImageField(upload_to='qrcode', blank=True, null=True)
qrcode_data = models.CharField(max_length=255, unique=True, blank=True)

class Meta:
unique_together = ('show', 'seat')

And its related Serializer

class MovieTicketSerializer(serializers.ModelSerializer):
class Meta:
model = MovieTicket
fields = '__all__'

To buy a new Ticket there's a view which is mapped to this url
*http://dev.site.com/api/movies/buy-ticket/
:*

@api_view(['POST'])@permission_classes([IsAuthenticated])def
buy_ticket(request):
serialized = MovieTicketSerializer(data=request.data)
if serialized.is_valid():
serialized.save()
return Response(serialized.data, status=status.HTTP_201_CREATED)
return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)

Now from the front end (Vue.js) I can create a new movie ticket like this:

const formBody = {
show: this.$store.state.showSelected.showTime.id,
user: this.$store.state.user.id,

// selectedSeats is an array of seats that have been selected by
the user. Here I am passing the first seat object.
seat: this.$store.state.selectedSeats[0].seat.id};
this.$http.post("http://dev.site.com/api/movies/buy-ticket/;, formBody)
.then(function (response) {
console.log(response.data);
})
.catch(function (response) {
console.log(response);
});return;

If the form was valid, this will create a new MovieTicket Object.

Now, suppose if the user selected multiple seats, I can loop through each
selectedSeats array and get the seat ids. But what I am confused is how can
I pass multiple seat.id if Django rest framework is only accepting one seat
per request?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWv7yXiCM2VAHU3MBb19nLADU49UH5ZONKW5C7YEUTtfmg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Extending a image and adding text on the extended area using python pil

2017-04-08 Thread Aamu Padi
I am creating QRcode for each ticket. The QRcode is in image format.

​I want to extend this image vertically on both the sides (i.e. Top and
Bottom). And in the extended area I want to add some additional data like
this:

Theater Name
_
||
||
|   QR CODE  |
||
||
||
||
||
||
Seat id: 24 C
Movie: The baby boss
Showtime: 2:30 pm
Date: 09/04/17



Now I am doing this way to add image:

image = Image.open('qrcode.jpg')
width, height = image.size
draw = ImageDraw.Draw(image)
text_cinema = "RPG Cinema"
text_seat = "Seat: 15C Balcony"
text_movie = "The baby boss"
text_showtime = "02:30 pm"
text_date = "09/04/17"
font = ImageFont.truetype('font.ttf', 24)
draw.text((40, 5), text_cinema, font=font)
draw.text((40,height - 40), text_seat, font=font)
draw.text((40,height + 40), text_movie, font=font)
draw.text((40,height + 40), text_showtime, font=font)
image.save(str(time()).replace('.', '_') + '.jpg')


And the image turns out to be in this way:


​

As you can see, the text are added into the image and not extending the
image itself. Also, other data like text_movie, text_showtime and text_date are
not being inserted as the text does not extend the image.

How can I extend the image and insert texts into the extended image area?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWun8vu1eAprO_46iRPsq-qJuejjsLkWPXc%2BgxjKFsp5cQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to create a new user with django rest framework and custom user model

2017-03-26 Thread Aamu Padi
I have a custom user model and I am using django-rest-framework to create
API

models.py:

class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
first_name = models.CharField(max_length=15)
last_name = models.CharField(max_length=15)
mobile = models.IntegerField(unique=True)
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)


serializers.py:

class UserSerializer(serializers.ModelSerializer):
password1 = serializers.CharField(write_only=True)
password2 = serializers.CharField(write_only=True)

class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'mobile',
'password1', 'password2')


views.py:

@api_view(['POST'])
@permission_classes((AllowAny,))
def create_user(request):
serialized = UserSerializer(data=request.data)
if serialized.is_valid():
User.objects.create_user(
serialized.save()
)
return Response(serialized.data, status=status.HTTP_201_CREATED)
else:
return Response(serialized._errors,
status=status.HTTP_400_BAD_REQUEST)


However, when I try to create a new user I am getting this error:

*Got a TypeError when calling User.objects.create(). This may be because
> you have a writable field on the serializer class that is not a valid
> argument to User.objects.create(). You may need to make the field
> read-only, or override the UserSerializer.create() method to handle this
> correctly.*


This maybe because there's no password1 or password2 fields in the User
model. But so, how can I create an API to create a new user using
django-rest-framework?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtWmifpo6BuWQmp3-Y-pXyJAPBELf90MmeWLQBB9nGmyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


django rest framework - request context key error

2016-06-21 Thread Aamu Padi
I have two models (Like and News). I am using django-rest-framework to make
a web api out of it.

class Like(models.Model):
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')

class News(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=150)
...
likes = GenericRelation(Like)


A *Like object* has its own user field to store who liked the News object.
Now to check if a specific user exists in any of the likes of a News
object, I am getting request.user from a SerializerMethodField.

class NewsSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer()
likes_count = serializers.IntegerField(source='likes.count', read_only=
True)
user_in_likes = serializers.SerializerMethodField()

class Meta:
model = News
fields = ('user', 'title', 'body', 'article_image', 'pub_date',
'likes_count', 'user_in_likes')

def get_user_in_likes(self, obj):
user = self.context['request'].user
what = obj.likes.filter(user=user).exists()
return what


When I go the /news/ url, I get the json objects including the
*user_in_likes* to true/false for each news object.

However, I have another serialzer for different model which imports
*NewsSerializer
class* and other similar serializers:

class ActivityObjectRelatedField(serializers.RelatedField):
def to_representation(self, value):
if isinstance(value, User):
serializer = UserSerializer(value)
elif isinstance(value, Job):
serializer = JobSerializer(value)
elif isinstance(value, News):
serializer = NewsSerializer(value)
elif isinstance(value, Tender):
serializer = TenderSerializer(value)
else:
raise Exception('Unexpected type of tagged object')
return serializer.data

class ActivitySerializer(serializers.HyperlinkedModelSerializer):
actor = ActivityObjectRelatedField(read_only=True)
target = ActivityObjectRelatedField(read_only=True)

class Meta:
model = Activity
fields = ('url', 'actor', 'verb', 'target', 'pub_date')


Now if I visit */activities/*, to get the activities objects I am getting
an error:

KeyError at /activities/

'request'


And it points to the line of SerializerMethod of NewsSerialize class where
*self.context['request'].user* is used.

Exception Location: /vagrant/myproject/news/serializers.py in
 get_user_in_likes, line 25


Again if I visit */news/* url, everything is fine and I get news objects.
What am I missing here? Why is *request* not being recognized in the
*ActivitiesSerializer
class*? Please help me solve this problem. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsBp3uXaMVzqLmHmyQGP3MtwO06nZ-GQE98523DAVD-8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


django-rest-framework serializer for ContentType object

2016-06-20 Thread Aamu Padi
I am building an activity model, somewhat similar to this package
. It has an actor,
verb and the target.

class Activity(models.Model):
actor_type = models.ForeignKey(ContentType,
related_name='actor_type_activities')
actor_id = models.PositiveIntegerField()
actor = GenericForeignKey('actor_type', 'actor_id')
verb = models.CharField(max_length=10)
target_type = models.ForeignKey(ContentType,
related_name='target_type_activities')
target_id = models.PositiveIntegerField()
target = GenericForeignKey('target_type', 'target_id')
pub_date = models.DateTimeField(default=timezone.now)


Now whenever a new object of whichever models (Tender, Job and News) is
created, a new Activity object is created, with the target being the
objects of any of these three models.

eg. user (actor) published (verb) title (target)

class Tender(models.Model):
title = models.CharField(max_length=256)
description = models.TextField()

class Job(models.Model):
title = models.CharField(max_length=256)
qualification = models.CharField(max_length=256)

class News(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=150)

To get this data I am making an API which will get me the required json
data. I am using django-rest-framework
 for this and very new with it.

class ActorSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email')

class ActivitySerializer(serializers.HyperlinkedModelSerializer):
actor = ActorSerializer()
class Meta:
model = Activity
fields = ('url', 'actor', 'verb', 'pub_date')

In the above serializers, I knew that *actor *will be the *User*. And so I
used the User model for the *ActorSerializer class*. But as for the *target*,
it can be any of these three models (News/Job/Tender).

How can I make a serializer (eg. TargetSerialier class) for the ContentType
object so that I can use the target in the ActivitySerializer class field?


Virus-free.
www.avast.com

<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuRvs6mJFwyMfzFXXVxjpF%3D_L6jcWJiH-Dir5pZZ5RX0Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Is using multi-table inheritance still a bad choice?

2016-06-17 Thread Aamu Padi
Hi,
I want to create feeds activity using the models that I have.

class Tender(models.Model):
title = models.CharField(max_length=256)
description = models.TextField()
department = models.CharField(max_length=50)
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)

class Job(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
qualification = models.CharField(max_length=256)
salary = models.IntegerField()
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)

class News(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=150)
body = models.TextField()
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)

In the view, I would want any of the latest 10 objects from tender/job/news
which ever was latest according to their *pub_date*. For this purpose,
using a multi-table inheritance sounds good. But I saw this post on
stackoverflow

about the pros/cons when using multi-table inheritance. Now even though it
was an old post, I am confused, whether to go with multi-table inheritance
or not. Your help and guidance will be very much appreciated.

Thank you.


Virus-free.
www.avast.com

<#m_-3681315383742066344_DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtGM0BbOpjmAZMTDJk4nooGAFgtJP6SUCaKRNnM1K8goA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Display objects from different models at the same page according to their published date

2016-06-10 Thread Aamu Padi
Yes, I too was thinking about that. I had doubts about the space and the
performance of it. Do you see any downside \on the space and performance?

On Fri, Jun 10, 2016 at 6:57 PM, Rafael Guillén <arthe...@gmail.com> wrote:

> Instead of making them entirely separate models, you can use one model as
> a base and have the other models inherit from them, then you display the
> base model. I think this might be a good fit for multi-table inheritance.
> See if this helps:
>
> https://docs.djangoproject.com/en/1.9/topics/db/models/#model-inheritance
>
>
> On Friday, June 10, 2016 at 6:59:55 AM UTC-5, Aamu Padi wrote:
>>
>> This is just a follow up of my question.
>>
>> I don't want to show each of the model objects separately at the same
>> page. But like feeds of facebook or any other social media. Suppose a new
>> Job object was created, and after that a new News object is created. Then,
>> I want to show the News object first, and then the Job object, and so on.
>>
>> On Fri, Jun 10, 2016 at 5:12 PM, Aamu Padi <aamu...@gmail.com> wrote:
>>
>>> I have three different models for my app. All are working as I expected.
>>>
>>> class Tender(models.Model):
>>> title = models.CharField(max_length=256)
>>> description = models.TextField()
>>> department = models.CharField(max_length=50)
>>> address = models.CharField(max_length=50)
>>> nature_of_work = models.CharField(choices=WORK_NATURE,
>>> max_length=1)
>>> period_of_completion = models.DateField()
>>> pubdat = models.DateTimeField(default=timezone.now)
>>>
>>> class Job(models.Model):
>>> user = models.ForeignKey(settings.AUTH_USER_MODEL)
>>> title = models.CharField(max_length=256)
>>> qualification = models.CharField(max_length=256)
>>> interview_type = models.CharField(max_length=2,
>>> choices=INTERVIEW_TYPE)
>>> type_of_job = models.CharField(max_length=1, choices=JOB_TYPE)
>>> number_of_vacancies = models.IntegerField()
>>> employer = models.CharField(max_length=50)
>>> salary = models.IntegerField()
>>> pubdat = models.DateTimeField(default=timezone.now)
>>>
>>> class News(models.Model):
>>> user = models.ForeignKey(settings.AUTH_USER_MODEL)
>>> title = models.CharField(max_length=150)
>>> body = models.TextField()
>>> pubdat = models.DateTimeField(default=timezone.now)
>>>
>>> Now I am displaying each of them at separate page for each of the model
>>> (e.g. in the *jobs* page, I am displaying only the jobs.). But now at the
>>> home page, I want to display these according to their published date at the
>>> same page. How can I display different objects from different models at the
>>> same page? Do I make a separate model e.g. `class Post` and then use signal
>>> to create a new post whenever a new object is created from `Tender`, or
>>> `Job`, or `News`? I  really hope there is a better way to achieve this.
>>> Please help me. Thank you.
>>>
>>>
>>> <https://www.avast.com/sig-email?utm_medium=email_source=link_campaign=sig-email_content=webmail>
>>>  Virus-free.
>>> www.avast.com
>>> <https://www.avast.com/sig-email?utm_medium=email_source=link_campaign=sig-email_content=webmail>
>>> <#m_-4072898299284482670_CAHSNPWuR7kHn7knMwj=C45t1qvDB4kRCBdE1+8aB-Zh0qeHyDA@mail.gmail.com_m_-8239736330560799881_DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d0ba3060-a7ae-4837-9d23-c5a652ef6bb5%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d0ba3060-a7ae-4837-9d23-c5a652ef6bb5%40googlegroups.com?utm_medium=email_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWviFAuAJLS_Mz0ruLnm2321TgV28f-BnSXfScmQ9%3D9jLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Display objects from different models at the same page according to their published date

2016-06-10 Thread Aamu Padi
I have three different models for my app. All are working as I expected.

class Tender(models.Model):
title = models.CharField(max_length=256)
description = models.TextField()
department = models.CharField(max_length=50)
address = models.CharField(max_length=50)
nature_of_work = models.CharField(choices=WORK_NATURE, max_length=1)
period_of_completion = models.DateField()
pubdat = models.DateTimeField(default=timezone.now)

class Job(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=256)
qualification = models.CharField(max_length=256)
interview_type = models.CharField(max_length=2,
choices=INTERVIEW_TYPE)
type_of_job = models.CharField(max_length=1, choices=JOB_TYPE)
number_of_vacancies = models.IntegerField()
employer = models.CharField(max_length=50)
salary = models.IntegerField()
pubdat = models.DateTimeField(default=timezone.now)

class News(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=150)
body = models.TextField()
pubdat = models.DateTimeField(default=timezone.now)

Now I am displaying each of them at separate page for each of the model
(e.g. in the *jobs* page, I am displaying only the jobs.). But now at the
home page, I want to display these according to their published date at the
same page. How can I display different objects from different models at the
same page? Do I make a separate model e.g. `class Post` and then use signal
to create a new post whenever a new object is created from `Tender`, or
`Job`, or `News`? I  really hope there is a better way to achieve this.
Please help me. Thank you.


Virus-free.
www.avast.com

<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWt%2BwkXBSxPr-O%3DWsCNJRfLK0EOUoji3nS%2BdMOWa3x5jDw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Custom page design by users

2015-08-20 Thread Aamu Padi
I am planning on building a blogging site. In that, I would like to allow
the users to customize there page using html tags and bootstrap.

1. How do I save the contents?

   - Save all the contents in the db?
   - Or create different html files and serve them?
   - Performance?


2. I was planning on using lxml  for stripping
dangerous codes( scripts/iframe).

   - Or should I use some other tool (bleach
   /BeautifulSoup
   ) or way of stripping the
   content?


3. What other security measures I should be taking?

Your opinions and suggestions are very important. Could you please help me
with making it possible. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWvNHWSq1jyrXeT00VGrLarVSh89UXGxmJT8yRK%2B_xKL4A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Guidance needed - Real time and django

2014-08-29 Thread Aamu Padi
Hello,
I would like to build a real time web application, but have little idea of
where to start to from. It will have notification and feed system like
facebook or instagram. Would really appreciate if anyone who have already
done this type of web application could kindly guide me through. I will be
very grateful.

Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuiOJ5m%3DcCUFOc-j%3DRR3LHSAOyhd4Uk7HOaR78cQB-V4A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to query multiple described objects exist in a many to many field

2013-12-05 Thread Aamu Padi
Thank you so much! That was a very helpful explanation. And it worked
perfectly!!!

Just, one more question please. Suppose, the user wants to send a message
to a group of users, how do I find the thread in this situation?

Eg: user1 wants to send a message to both the user2 and user3. How to find
the thread, in this case '*t3*'?

>>> t1 = Thread.objects.get(id=1)
>>> t1
[,]
>>> t2 = Thread.objects.get(id=2)
>>> t2
[,]
>>> t3 = Thread.objects.get(id=3)
>>> t3
[,,]


On Wed, Dec 4, 2013 at 5:41 PM, Tom Evans <tevans...@googlemail.com> wrote:

> On Wed, Dec 4, 2013 at 10:24 AM, Aamu Padi <aamup...@gmail.com> wrote:
> > How do I check whether there is a thread containing only the sender
> (user 1)
> > and the recipient (user 2), and no other users.
> >
> > models.py
> >
> > class Thread(models.Model):
> > user = models.ManyToManyField(User)
> > is_hidden = models.ManyToManyField(User,
> > related_name='hidden_thread', blank=True)
> >
> > class Message(models.Model):
> > thread = models.ForeignKey(Thread)
> > sent_date = models.DateTimeField(default=datetime.now)
> > sender = models.ForeignKey(User)
> > body = models.TextField()
> > is_hidden = models.ManyToManyField(User,
> > related_name='hidden_message', blank=True)
> >
> > I tried
> >
> > >>> Thread.objects.filter(user=user1)
> > >>> Thread.objects.filter(user=user1|user2)
> > # Both gave me an error:  Unsupported operand.
> >
> > # Then this
> > >>> Thread.objects.filter(Q(user=user1) & Q(user=user2))
> > # Which gave me no threads at all.
> >
> > # Then this
> > >>> Thread.objects.filter(Q(user=user1) | Q(user=user2)).distinct()
> > # Gave me threads of both the users.
> >
> > What I want is to check the thread, with the specified users only.
> Suppose,
> > User 1 wants to send a message to User 2. What I want is, first check
> > whether there is a thread between both the users. If there is, get that
> > thread, or else create a new one. How is it possible? What is the best
> way
> > to do it. Please help me. Thank you.
>
> I hope you understand SQL a little, as it will be required to follow
> this explanation.
>
> The condition "Thread.objects.filter(user=..)" goes across a join to
> the users<->threads "through" table, which has columns (id, thread_id,
> user_id).
>
> When you specify conditions in a "filter()" call, each of the
> conditions specified in that call is AND'ed together (the equivalent
> of what you were doing in the "Q(user=user1) & Q(user=user2)", django
> would do that for you). However, what does that mean, in terms of SQL?
>
> Well, it means "find me rows from the users<->threads through table
> that have user_id=user1.pk and user_id=user2.pk". Obviously, this will
> never find results unless user1==user2.
>
> So, you need to specify that you want to join twice to that table. You
> do this by specifying the conditions for each join in a separate
> "filter()" call, and so to find threads that have a row with user1 in
> the through table, and also have user2 in the through table:
>
> Thread.objects.filter(user=user1).filter(user=user2)
>
> You started by asking how to find threads which had user1 and user2,
> but no other users. This is more complex, you need to count the number
> of users in the thread without filtering the users first! This means
> two queries, or a sub query:
>
> threads_with_both = Thread.objects.filter(user=user1).filter(user=user2)
> threads_with_only_both = Thread.objects.filter(
>  pk__in=threads_with_both).annotate(
>  num_users=Count('user')).filter(
>  num_users=2)
>
> >
> > And please tell me what's the difference between | and &? Because I had
> very
> > different results with these two.
>
> | is OR
> & is AND
>
> So to find threads with either user1 or user2 (or both):
>
> Thread.objects.filter(Q(user=user1) | Q(user=user2))
>
> To find hidden threads with user1
>
> Thread.objects.filter(Q(user=user1) & Q(is_hidden=True))
>
> which is the same as
>
> Thread.objects.filter(user=user1, is_hidden=True)
>
>
> Cheers
>
> Tom
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to djan

How to query multiple described objects exist in a many to many field

2013-12-04 Thread Aamu Padi
How do I check whether there is a thread containing only the sender (user
1) and the recipient (user 2), and no other users.

models.py

class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User,
related_name='hidden_thread', blank=True)

class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User,
related_name='hidden_message', blank=True)

I tried

>>> Thread.objects.filter(user=user1)
>>> Thread.objects.filter(user=user1|user2)
# Both gave me an error:  Unsupported operand.

# Then this
>>> Thread.objects.filter(Q(user=user1) & Q(user=user2))
# Which gave me no threads at all.

# Then this
>>> Thread.objects.filter(Q(user=user1) | Q(user=user2)).distinct()
# Gave me threads of both the users.

What I want is to check the thread, with the specified users only. Suppose,
User 1 wants to send a message to User 2. What I want is, first check
whether there is a thread between both the users. If there is, get that
thread, or else create a new one. How is it possible? What is the best way
to do it. Please help me. Thank you.

And please tell me what's the difference between | and &? Because I had
very different results with these two.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtFESu7WGZcHDOeNCVc1FqOqxqw2kkiRY0jaJctXy6XHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


[no subject]

2013-12-03 Thread Aamu Padi
How do I check whether there is a *thread* containing only the *sender
*(user 1) and the
*recipient* (user 2), and no other users.

models.py

class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User,
related_name='hidden_thread', blank=True)

class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User,
related_name='hidden_message', blank=True)

I tried

>>> Thread.objects.filter(user=user1)
>>> Thread.objects.filter(user=user1|user2)
*# Both gave me an error:  Unsupported operand.*

*# Then this*
>>> Thread.objects.filter(Q(user=user1) | Q(user=user2))
*# Which gave me all the threads of user1 and user2*

*# Then this*
>>> Thread.objects.filter(Q(user=user1) | Q(user=user2)).distinct()
*# Just the same as the above, just without repeating the same threads.*

What I want is to check the thread, with the specified users only. Suppose,
User 1 wants to send a message to User 2. First it must check whether there
is a thread between both the users. If there is, get that thread, or else
create a new one. How is it possible? Please help me. Thank you.

And please tell me what's the difference between `|` and `&`? Because I had
very different results with these two.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWueRVf2JghkgncVQvPNOtUMbZ9j0o0rzC9uu%2B9ZrptHyg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Save the user from one model to the another model

2013-12-03 Thread Aamu Padi
Thank you all so much for the answers! I came up with this solution.

class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User, related_name='hidden_message',
blank=True)

def __unicode__(self):
return "%s - %s - %s" % (unicode(self.thread.id), self.body,
self.sender)

def save(self, *args, **kwargs):
probably_new = (self.pk is None)
super(Message, self).save(*args, **kwargs)
if probably_new:
self.thread.user.add(self.sender)

Please correct me, if this is not a good option. But with signals I can
check whether its a new created message or not. Does this have any draw
backs? Thank you once again!


On Tue, Dec 3, 2013 at 8:22 PM, antialiasis <antialia...@gmail.com> wrote:

> I believe what Aamu Padi is getting at is that he doesn't want to do this
> in the view at all. He just wants it to happen *whenever* a Message is
> created. That's a textbook use case for signals.
>
> On Tuesday, December 3, 2013 1:14:50 PM UTC, Timothy W. Cook wrote:
>>
>> On Tue, Dec 3, 2013 at 9:56 AM, Aamu Padi <aamu...@gmail.com> wrote:
>> > Ok! Plural names. Got it. But I was looking for something more of a
>> > overriding the save method for the Thread class, so that I don't have
>> to do
>> > that in the view.
>> >
>>
>> You can do it in the same view as the message.  You aren't limited to
>> working with only one model in a view.
>>
>> HTH,
>> Tim
>>
>>
>>
>>
>> >
>> > On Tue, Dec 3, 2013 at 1:39 PM, <jirka.v...@gmail.com> wrote:
>> >>
>> >> You're probably looking for something along the lines of:
>> >>
>> >> thread = Thread.objects.get(id=)
>> >> msg = Message.objects.get(id=)
>> >>
>> >> thread.user.add(msg.sender)
>> >>
>> >> Look at add() and remove() methods in documentation (search for
>> related
>> >> managers).
>> >>
>> >> FYI - it's a good practice to name fields that use ManyToMany field in
>> >> plural, i.e. Thread.users would be better than Thread.user
>> >>
>> >> HTH
>> >>
>> >> Jirka
>> >> 
>> >> From: Aamu Padi <aamu...@gmail.com>
>> >> Sender: django...@googlegroups.com
>> >> Date: Tue, 3 Dec 2013 04:19:55 +0530
>> >> To: <django...@googlegroups.com>
>> >> ReplyTo: django...@googlegroups.com
>> >> Subject: Save the user from one model to the another model
>> >>
>> >> What I want to do is, whenever I create a new message, I want the
>> sender
>> >> of the Message to be added to the user of the Thread. How do I do
>> that?
>> >>
>> >>>
>> >>> class Thread(models.Model):
>> >>> user = models.ManyToManyField(User)
>> >>> is_hidden = models.ManyToManyField(User,
>> >>> related_name='hidden_thread', blank=True)
>> >>>
>> >>> def __unicode__(self):
>> >>> return unicode(self.id)
>> >>>
>> >>> class Message(models.Model):
>> >>> thread = models.ForeignKey(Thread)
>> >>> sent_date = models.DateTimeField(default=datetime.now)
>> >>> sender = models.ForeignKey(User)
>> >>> body = models.TextField()
>> >>> is_hidden = models.ManyToManyField(User,
>> >>> related_name='hidden_message', blank=True)
>> >>>
>> >>> def __unicode__(self):
>> >>> return "%s - %s" % (unicode(self.thread.id), self.body)
>> >>>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> Groups
>> >> "Django users" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> an
>> >> email to django-users...@googlegroups.com.
>> >> To post to this group, send email to django...@googlegroups.com.
>> >> Visit this group at http://groups.google.com/group/django-users.
>> >> To view this discussion on the web visit
>> >> https://groups.google.com/d/msgid/django-users/
>> CAHSNPWveL8rqDLenDLayGrtC_RAwQ-Pfp0SYravr7%2BuJzhBhXw%40mail.gmail.com.
>> >> For more options, visit https://groups.google.com/gro

Re: Save the user from one model to the another model

2013-12-03 Thread Aamu Padi
Ok! Plural names. Got it. But I was looking for something more of a
overriding the save method for the *Thread *class, so that I don't have to
do that in the view.


On Tue, Dec 3, 2013 at 1:39 PM, <jirka.vejra...@gmail.com> wrote:

> You're probably looking for something along the lines of:
>
> thread = Thread.objects.get(id=)
> msg = Message.objects.get(id=)
>
> thread.user.add(msg.sender)
>
> Look at add() and remove() methods in documentation (search for related
> managers).
>
> FYI - it's a good practice to name fields that use ManyToMany field in
> plural, i.e. Thread.users would be better than Thread.user
>
> HTH
>
> Jirka
> --
> *From: * Aamu Padi <aamup...@gmail.com>
> *Sender: * django-users@googlegroups.com
> *Date: *Tue, 3 Dec 2013 04:19:55 +0530
> *To: *<django-users@googlegroups.com>
> *ReplyTo: * django-users@googlegroups.com
> *Subject: *Save the user from one model to the another model
>
> What I want to do is, whenever I create a new message, I want the *sender
> *of the *Message* to be added to the *user *of the *Thread.* How do I do
> that?
>
>
>> class Thread(models.Model):
>> user = models.ManyToManyField(User)
>> is_hidden = models.ManyToManyField(User,
>> related_name='hidden_thread', blank=True)
>>
>> def __unicode__(self):
>> return unicode(self.id)
>>
>> class Message(models.Model):
>> thread = models.ForeignKey(Thread)
>> sent_date = models.DateTimeField(default=datetime.now)
>> sender = models.ForeignKey(User)
>> body = models.TextField()
>> is_hidden = models.ManyToManyField(User,
>> related_name='hidden_message', blank=True)
>>
>> def __unicode__(self):
>> return "%s - %s" % (unicode(self.thread.id), self.body)
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHSNPWveL8rqDLenDLayGrtC_RAwQ-Pfp0SYravr7%2BuJzhBhXw%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/495305343-1386058174-cardhu_decombobulator_blackberry.rim.net-1176089190-%40b17.c3.bise7.blackberry
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtJ80rqeDgJU4x3xk%2B3ABR1SKjA%2BohsPa3cPezx8O%3Darg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Save the user from one model to the another model

2013-12-02 Thread Aamu Padi
What I want to do is, whenever I create a new message, I want the *sender *of
the *Message* to be added to the *user *of the *Thread.* How do I do that?


> class Thread(models.Model):
> user = models.ManyToManyField(User)
> is_hidden = models.ManyToManyField(User, related_name='hidden_thread',
> blank=True)
>
> def __unicode__(self):
> return unicode(self.id)
>
> class Message(models.Model):
> thread = models.ForeignKey(Thread)
> sent_date = models.DateTimeField(default=datetime.now)
> sender = models.ForeignKey(User)
> body = models.TextField()
> is_hidden = models.ManyToManyField(User,
> related_name='hidden_message', blank=True)
>
> def __unicode__(self):
> return "%s - %s" % (unicode(self.thread.id), self.body)
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWveL8rqDLenDLayGrtC_RAwQ-Pfp0SYravr7%2BuJzhBhXw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: auto populating the fields in the models

2013-12-01 Thread Aamu Padi
Yes, that's what I was looking for, the save method. Can you please kindly
show me how, so that I don't have to create a new thread first or create a
new set of ThreadParticipants. I just want it to be on the background, so
that all I have to do is create a message and send it to the user.
Thank you.


On Sun, Dec 1, 2013 at 9:14 PM, Rafael E. Ferrero
<rafael.ferr...@gmail.com>wrote:

> sorry my previous message... it was not quite clear... here again:
>
>
>
> 2013/12/1 Rafael E. Ferrero <rafael.ferr...@gmail.com>
>
> IMHO with this design you first need an user and a thread to create a new
>> message.
>>
>> In the admin interface you gonna look a + button to create a user and
>> other to create a thread.
>>
>
>
> OR
>
> You can redefine the save method of your model. For example:
>
>
>>
>> For example
>>
>> class Message(models.Model):
>> thread = models.ForeignKey(Thread)
>> sent_date = models.DateTimeField(default=datetime.now)
>> body = models.TextField()
>> user = models.ForeignKey(User)
>>
>> def save(self):
>> if not self.id:
>>     #on a new message do something
>> #on modify message do somenthing else
>> super(Message, self).save()
>>
>>
>>
>>
>>
>>
>> 2013/12/1 Aamu Padi <aamup...@gmail.com>
>>
>>> Hello, please have a look at my models.py.
>>>
>>> *models.py:*
>>>
>>>> class Thread(models.Model):
>>>> pass
>>>>
>>>> class ThreadParticipant(models.Model):
>>>> thread = models.ForeignKey(Thread)
>>>> user = models.ForeignKey(User)
>>>>
>>>> class Message(models.Model):
>>>> thread = models.ForeignKey(Thread)
>>>> sent_date = models.DateTimeField(default=datetime.now)
>>>> body = models.TextField()
>>>> user = models.ForeignKey(User)
>>>>
>>>> class MessageReadState(models.Model):
>>>> message = models.ForeignKey(Message)
>>>> user = models.ForeignKey(User)
>>>> read_date = models.DateTimeField()
>>>>
>>> I am having two problems when I try to create a new message:
>>>
>>>1. How do I auto populate the Thread with its primary key whenever I
>>>create a new message, without manually creating a new thread?
>>>2. How to create a new user if the user is not in the the
>>>ThreadParticipant, or else don't create a new user?
>>>
>>> I think I can solve this all in the views.py, but I think it will be
>>> better to solve this in the models.py. Please help me solve the problem. I
>>> would be very grateful. Thank you.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAHSNPWsLC1LW1pqpYe-FUmC4k4twxBV8HrLxRex4e6VNicoBdQ%40mail.gmail.com
>>> .
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>
>>
>> --
>> Rafael E. Ferrero
>>
>
>
>
> --
> Rafael E. Ferrero
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJJc_8VJCH01_SQZKOkAz%3DhwHgC%3Dsj-w%3DKLQTxJc1K92RZ11jw%40mail.gmail.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsSy0mTPmp8zv_X0whCKu7nPHnFcPb%3DMs6YFPmQbYriMg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


auto populating the fields in the models

2013-12-01 Thread Aamu Padi
Hello, please have a look at my models.py.

*models.py:*

> class Thread(models.Model):
> pass
>
> class ThreadParticipant(models.Model):
> thread = models.ForeignKey(Thread)
> user = models.ForeignKey(User)
>
> class Message(models.Model):
> thread = models.ForeignKey(Thread)
> sent_date = models.DateTimeField(default=datetime.now)
> body = models.TextField()
> user = models.ForeignKey(User)
>
> class MessageReadState(models.Model):
> message = models.ForeignKey(Message)
> user = models.ForeignKey(User)
> read_date = models.DateTimeField()
>
I am having two problems when I try to create a new message:

   1. How do I auto populate the Thread with its primary key whenever I
   create a new message, without manually creating a new thread?
   2. How to create a new user if the user is not in the the
   ThreadParticipant, or else don't create a new user?

I think I can solve this all in the views.py, but I think it will be better
to solve this in the models.py. Please help me solve the problem. I would
be very grateful. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsLC1LW1pqpYe-FUmC4k4twxBV8HrLxRex4e6VNicoBdQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to use primary key as an attribute

2013-12-01 Thread Aamu Padi
You are really are a life saver. Thank you so much sir. I have posted a
more specific question. Please kindly have a look at it.
Thank you.


On Sun, Dec 1, 2013 at 5:09 PM, Timothy W. Cook <t...@mlhim.org> wrote:

> If I understand what you are building.  You want some way to group
> messages into threads? I assume that a Thread will have many messages?
>  Unless I am missing something you might want something like this:
>
> class Thread(models.Model):
> subject = models.CharField(max_length=256)
>
> class Message(models.Model):
> thread = models.ForeignKey(Thread)
> sent_date = models.DateTimeField(default=datetime.now)
> body = models.TextField()
> user = models.ForeignKey(User)
>
> Then you can create a filter for the Thread based on a subject:
>
> selected_subject = Thread.objects.filter(subject="some subject")
>
> Then when you create the message you can use thread =
> selected_subject[0] (assuming unique subjects/threads) when you create
> your message. OR if 'some subject' isn't found,  len(selected_subject)
> == 0 then create a new Thread.
>
> HTH,
> Tim
>
>
>
> On Sun, Dec 1, 2013 at 8:30 AM, Aamu Padi <aamup...@gmail.com> wrote:
> > Actually I need to use Thread class in other class as a ForeignKey. Here
> is
> > the whole code:
> >
> > class Thread(models.Model):
> > thread_pk = models.PositiveIntegerField(default=self.pk)
> >
> > class ThreadParticipant(models.Model):
> > thread = models.ForeignKey(Thread)
> > user = models.ForeignKey(User)
> >
> > class Message(models.Model):
> > thread = models.ForeignKey(Thread)
> > sent_date = models.DateTimeField(default=datetime.now)
> > body = models.TextField()
> > user = models.ForeignKey(User)
> >
> >
> > Is there anyway to create a new message without manually creating a
> Thread.
> > I mean to auto-populate the Thread, whenever I create a new Message?
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to django-users+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-users@googlegroups.com.
> > Visit this group at http://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/CAHSNPWt14jxduA_kT-Yxj-7k02qZzZkvkbfYU2pEPFghbNq%3DEg%40mail.gmail.com
> .
> >
> > For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> MLHIM VIP Signup: http://goo.gl/22B0U
> 
> Timothy Cook, MSc   +55 21 94711995
> MLHIM http://www.mlhim.org
> Like Us on FB: https://www.facebook.com/mlhim2
> Circle us on G+: http://goo.gl/44EV5
> Google Scholar: http://goo.gl/MMZ1o
> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3XsrpZ5U-2626-r4dTvQgoeXUiKY%3Dvx1Yp5dPjHKYnr0A%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuWKPRLk%3DOon3gFxELjoyzQ_1WVFEZjW00D4%3DLgUpF4ZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to get the class name of a ContentType in django

2013-12-01 Thread Aamu Padi
Ok, I will try it! Thank you so very much!!!


On Sun, Dec 1, 2013 at 3:41 AM, Timothy W. Cook <t...@mlhim.org> wrote:

> On Sat, Nov 30, 2013 at 3:37 PM, Aamu Padi <aamup...@gmail.com> wrote:
> > Yes!!! Can I use it as a string in template?
> >
>
>
> I haven't tested it but it seems you should be able to assign it to a
> context variable or kwargs in a view and use it in a template.
>
> Something like:
>
> context['classname'] = type(myinstance)
> or
> kwargs['classname'] = type(myinstance)
>
> HTH,
> Tim
>
> >
> > On Sat, Nov 30, 2013 at 2:45 PM, Timothy W. Cook <t...@mlhim.org> wrote:
> >>
> >> You can use type() on your instance(s) to find out their model class.
> >>
> >> If you have an instance of the model:
> >>
> >> stream = StreamItem.objects.create(user,ct,oid,pdate)
> >>
> >> Then type(stream) should return 
> >>
> >> Is that what you wanted?
> >>
> >>
> >>
> >>
> >>
> >> On Sat, Nov 30, 2013 at 1:14 AM, Simon Charette <charett...@gmail.com>
> >> wrote:
> >> > Do you want to retrieve the class name of the model class associated
> >> > with a
> >> > content type?
> >> >
> >> > Le jeudi 28 novembre 2013 12:04:34 UTC-5, Aamu Padi a écrit :
> >> >>
> >> >> How do I get the class name in string of a ContentType? I tried it
> this
> >> >> way, but it didn't worked out:
> >> >>
> >> >> class StreamItem(models.Model):
> >> >> user = models.ForeignKey(User)
> >> >> content_type = models.ForeignKey(ContentType)
> >> >> object_id = models.PositiveIntegerField()
> >> >> pub_date = models.DateTimeField(default=datetime.now)
> >> >>
> >> >> content_object = generic.GenericForeignKey('content_type',
> >> >> 'object_id')
> >> >> content_class = content_type.__name__
> >> >>
> >> >> def __unicode__(self):
> >> >> return self.content_class
> >> >>
> >> >> Any help will be much appreciated! Thank you.
> >> >
> >> > --
> >> > You received this message because you are subscribed to the Google
> >> > Groups
> >> > "Django users" group.
> >> > To unsubscribe from this group and stop receiving emails from it, send
> >> > an
> >> > email to django-users+unsubscr...@googlegroups.com.
> >> > To post to this group, send email to django-users@googlegroups.com.
> >> > Visit this group at http://groups.google.com/group/django-users.
> >> > To view this discussion on the web visit
> >> >
> >> >
> https://groups.google.com/d/msgid/django-users/46096fde-2754-49aa-af03-3829a5d1be45%40googlegroups.com
> .
> >> > For more options, visit https://groups.google.com/groups/opt_out.
> >>
> >>
> >>
> >> --
> >> MLHIM VIP Signup: http://goo.gl/22B0U
> >> 
> >> Timothy Cook, MSc   +55 21 94711995
> >> MLHIM http://www.mlhim.org
> >> Like Us on FB: https://www.facebook.com/mlhim2
> >> Circle us on G+: http://goo.gl/44EV5
> >> Google Scholar: http://goo.gl/MMZ1o
> >> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "Django users" group.
> >> To unsubscribe from this group and stop receiving emails from it, send
> an
> >> email to django-users+unsubscr...@googlegroups.com.
> >> To post to this group, send email to django-users@googlegroups.com.
> >> Visit this group at http://groups.google.com/group/django-users.
> >> To view this discussion on the web visit
> >>
> https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3XfRZy7XmTwof5QkNB3oe%2BhF0PVJO_XW8SWi9QdxqnFRg%40mail.gmail.com
> .
> >>
> >> For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to django-users+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-users@googlegroups.com.
> > Visit this group 

Re: How to use primary key as an attribute

2013-12-01 Thread Aamu Padi
Actually I need to use *Thread* class in other class as a ForeignKey. Here
is the whole code:

class Thread(models.Model):
thread_pk = models.PositiveIntegerField(default=self.pk)

class ThreadParticipant(models.Model):
thread = models.ForeignKey(Thread)
user = models.ForeignKey(User)

class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
body = models.TextField()
user = models.ForeignKey(User)


Is there anyway to create a new message without manually creating a *Thread*.
I mean to auto-populate the Thread, whenever I create a new *Message*?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWt14jxduA_kT-Yxj-7k02qZzZkvkbfYU2pEPFghbNq%3DEg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


How to use primary key as an attribute

2013-11-30 Thread Aamu Padi
This may be a lame question, but how do I use the primary key as a default
value for an attribute?

This is my models.py:


*class Thread(models.Model):thread_pk =
models.PositiveIntegerField(default=self.pk )*

This gives me an error:

*self is not defined*

How do I go about using the pk as a default value of an attribute? Any help
will be grateful. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWs3NSQtzciqYL%2B7C7FL3Ybu1SAQv-L73EL2DNCMpJn7tA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to get the class name of a ContentType in django

2013-11-30 Thread Aamu Padi
Yes!!! Can I use it as a string in template?


On Sat, Nov 30, 2013 at 2:45 PM, Timothy W. Cook <t...@mlhim.org> wrote:

> You can use type() on your instance(s) to find out their model class.
>
> If you have an instance of the model:
>
> stream = StreamItem.objects.create(user,ct,oid,pdate)
>
> Then type(stream) should return 
>
> Is that what you wanted?
>
>
>
>
>
> On Sat, Nov 30, 2013 at 1:14 AM, Simon Charette <charett...@gmail.com>
> wrote:
> > Do you want to retrieve the class name of the model class associated
> with a
> > content type?
> >
> > Le jeudi 28 novembre 2013 12:04:34 UTC-5, Aamu Padi a écrit :
> >>
> >> How do I get the class name in string of a ContentType? I tried it this
> >> way, but it didn't worked out:
> >>
> >> class StreamItem(models.Model):
> >> user = models.ForeignKey(User)
> >> content_type = models.ForeignKey(ContentType)
> >> object_id = models.PositiveIntegerField()
> >> pub_date = models.DateTimeField(default=datetime.now)
> >>
> >> content_object = generic.GenericForeignKey('content_type',
> >> 'object_id')
> >> content_class = content_type.__name__
> >>
> >> def __unicode__(self):
> >> return self.content_class
> >>
> >> Any help will be much appreciated! Thank you.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to django-users+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-users@googlegroups.com.
> > Visit this group at http://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/46096fde-2754-49aa-af03-3829a5d1be45%40googlegroups.com
> .
> > For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> MLHIM VIP Signup: http://goo.gl/22B0U
> 
> Timothy Cook, MSc   +55 21 94711995
> MLHIM http://www.mlhim.org
> Like Us on FB: https://www.facebook.com/mlhim2
> Circle us on G+: http://goo.gl/44EV5
> Google Scholar: http://goo.gl/MMZ1o
> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3XfRZy7XmTwof5QkNB3oe%2BhF0PVJO_XW8SWi9QdxqnFRg%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsrPg8jC_hmSmgZfuO8KSucjCcb_V4h2PdePUxLwSK3oA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to get the class name of a ContentType in django

2013-11-30 Thread Aamu Padi
Yes. Could you kindly please show me how?


On Sat, Nov 30, 2013 at 8:44 AM, Simon Charette <charett...@gmail.com>wrote:

> Do you want to retrieve the class name of the model class associated with
> a content type?
>
> Le jeudi 28 novembre 2013 12:04:34 UTC-5, Aamu Padi a écrit :
>
>> How do I get the class name in string of a ContentType? I tried it this
>> way, but it didn't worked out:
>>
>> class StreamItem(models.Model):
>> user = models.ForeignKey(User)
>> content_type = models.ForeignKey(ContentType)
>> object_id = models.PositiveIntegerField()
>> pub_date = models.DateTimeField(default=datetime.now)
>>
>> content_object = generic.GenericForeignKey('content_type',
>> 'object_id')
>> content_class = content_type.__name__
>>
>> def __unicode__(self):
>> return self.content_class
>>
>> Any help will be much appreciated! Thank you.
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/46096fde-2754-49aa-af03-3829a5d1be45%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuYzjcqqzfrocSzWUoTBG%2BkuhSjk2f%2BRUk%3DMv0U1HAubg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Signals not working as expected

2013-11-29 Thread Aamu Padi
Thank you for the answer. I am a newbie, would be very grateful if you
could please show me how to do that?


On Fri, Nov 29, 2013 at 9:28 PM, Jérôme Thiard <jerome.thi...@gmail.com>wrote:

> From Django documentation
> https://docs.djangoproject.com/en/1.6/topics/signals/#connecting-receiver-functions
>
> You can put signal handling and registration code anywhere you like.
>> However, you’ll need to make sure that the module it’s in gets imported
>> early on so that the signal handling gets registered before any signals
>> need to be sent. This makes your app’s models.py a good place to put
>> registration of signal handlers.
>
>
>  I think you have to put your connection code in models.py (your
> `create_stream_item` function can still live in signals.py) in order to be
> sure connections are made early enough.
>
> 2013/11/28 Aamu Padi <aamup...@gmail.com>
>
>> I am trying to create a project for creating feeds/activity feeds of a
>> user with the help of a 
>> blog<http://ryanberg.net/blog/2008/jun/24/basics-creating-tumblelog-django/>
>> .
>>
>> These are the models -
>>
>> class StreamItem(models.Model):
>>> user = models.ForeignKey(User)
>>>  content_type = models.ForeignKey(ContentType)
>>> object_id = models.PositiveIntegerField()
>>> pub_date = models.DateTimeField(default=datetime.now)
>>> content_object = generic.GenericForeignKey('content_type',
>>> 'object_id')
>>>
>>> @property
>>> def content_class(self):
>>> return self.content_type.model
>>>
>>>
>>> class Blog(models.Model):
>>> user = models.ForeignKey(User)
>>> title = models.CharField(max_length=300)
>>> body = models.TextField()
>>> pub_date = models.DateTimeField(default=datetime.now)
>>>
>>>
>>> class Photo(models.Model):
>>> user = models.ForeignKey(User)
>>> title = models.CharField(max_length=200)
>>> image = models.ImageField(upload_to=get_upload_file_name)
>>>  pub_date = models.DateTimeField(default=datetime.now)
>>>
>>> And this is the signals.py:
>>>
>>> from django.db.models import signals
>>> from django.contrib.contenttypes.models import ContentType
>>> from django.dispatch import dispatcher
>>> from blogs.models import Blog
>>> from picture.models import Photo
>>> from models import StreamItem
>>>
>>> def create_stream_item(sender, instance, signal, *args, **kwargs):
>>>
>>>  # Check to see if the object was just created for the first time
>>>
>>> if 'created' in kwargs:
>>>  if kwargs['created']:
>>>  create = True
>>>
>>>  # Get the instance's content type
>>>
>>>  ctype = ContentType.object.get_for_model(instance)
>>>
>>>  if create:
>>>  si =
>>> StreamItem.objects.get_or_create(content_type=ctype, object_id=
>>> instance.id, pub_date = instance.pub_date)
>>>
>>>  # Send a signal on post_save for each of these models
>>>
>>> for modelname in [Blog, Photo]:
>>> dispatcher.connect(create_stream_item, signal=signals.post_save,
>>> sender=modelname)
>>>
>>
>> When I create a blog or upload a photo, the `signal` does not work. But I
>> can manually add items to the `StreamItem` app using the admin, and the
>> StreamItem does work as I want it to be. I think there's problem with the
>> signals.py. Please help me out. Would be much appreciate. Thank you.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAHSNPWtGQPpE7eexnis6EpC2-_WtLcf0ZY-uhvuschj7Tg3MVA%40mail.gmail.com
>> .
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users&

django - model for a messaging app between users

2013-11-29 Thread Aamu Padi
How do I go about creating a message app, between users. What's the
business logic for creating the model? All I can think of was like this:

models.py


>
>
>
>
> *class Message(models.Model):description =
> models.TextField()date_added =
> models.DateTimeField(default=datetime.now)sender =
> models.ForeignKey(User)recipient = models.ForeignKey(User)*


I am not very sure if this is the way to go. If you could kindly guide me
on how to get started, will be very thank full. Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtZaFs0%2BWSiZKmCnykDRBSo02Uru92_G1yC7ztzjmvjhw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Signals not working as expected

2013-11-28 Thread Aamu Padi
I am trying to create a project for creating feeds/activity feeds of a user
with the help of a
blog
.

These are the models -

class StreamItem(models.Model):
> user = models.ForeignKey(User)
>  content_type = models.ForeignKey(ContentType)
> object_id = models.PositiveIntegerField()
> pub_date = models.DateTimeField(default=datetime.now)
> content_object = generic.GenericForeignKey('content_type',
> 'object_id')
>
> @property
> def content_class(self):
> return self.content_type.model
>
>
> class Blog(models.Model):
> user = models.ForeignKey(User)
> title = models.CharField(max_length=300)
> body = models.TextField()
> pub_date = models.DateTimeField(default=datetime.now)
>
>
> class Photo(models.Model):
> user = models.ForeignKey(User)
> title = models.CharField(max_length=200)
> image = models.ImageField(upload_to=get_upload_file_name)
>  pub_date = models.DateTimeField(default=datetime.now)
>
> And this is the signals.py:
>
> from django.db.models import signals
> from django.contrib.contenttypes.models import ContentType
> from django.dispatch import dispatcher
> from blogs.models import Blog
> from picture.models import Photo
> from models import StreamItem
>
> def create_stream_item(sender, instance, signal, *args, **kwargs):
>
>  # Check to see if the object was just created for the first time
>
> if 'created' in kwargs:
>  if kwargs['created']:
>  create = True
>
>  # Get the instance's content type
>
>  ctype = ContentType.object.get_for_model(instance)
>
>  if create:
>  si =
> StreamItem.objects.get_or_create(content_type=ctype, object_id=instance.id,
> pub_date = instance.pub_date)
>
>  # Send a signal on post_save for each of these models
>
> for modelname in [Blog, Photo]:
> dispatcher.connect(create_stream_item, signal=signals.post_save,
> sender=modelname)
>

When I create a blog or upload a photo, the `signal` does not work. But I
can manually add items to the `StreamItem` app using the admin, and the
StreamItem does work as I want it to be. I think there's problem with the
signals.py. Please help me out. Would be much appreciate. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtGQPpE7eexnis6EpC2-_WtLcf0ZY-uhvuschj7Tg3MVA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Admin File CSS Not Working Properly

2013-11-28 Thread Aamu Padi
Ok, you are using WAMP, so Apache as the server. Right? If so, have you
tried this command --

manage.py collectstatic

And then run the server again.
Hope this helped.


Cheers!


On Fri, Nov 29, 2013 at 2:22 AM, Aamu Padi <aamup...@gmail.com> wrote:

> Are you running the development server?
>
>
> On Fri, Nov 29, 2013 at 12:30 AM, nagendra palla <nagpalla...@gmail.com>wrote:
>
>> Hi Developers,
>>
>>I am new to Django App Development. My part-1 training is completed
>> successfully. But when i go for part-2, my Admin page is not working
>> properly. i.e., Admin page is loading with out CSS.
>>
>>
>> <https://lh3.googleusercontent.com/-UvH3Gs6hhp4/UpeRZsfJDrI/ADk/VugsMR9cAP4/s1600/Capture.JPG>
>>
>> When i am open admin page it showing like above. I am running my django
>> in Windows 8.
>>
>> What Software, i am used is:
>>
>> Python 2.7, Django 1.6, WAMP 2.4, WSGI
>>
>> and please find the attachment. i am attaching my project folder also.
>> Please Help me Any one in this group.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/e145c19f-6553-4efd-a1ae-8072d37f49b4%40googlegroups.com
>> .
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWseDm1%3DXiA_pmfieEmScY%2BX_84aTXD0qGsXcAaXo5xbMg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Admin File CSS Not Working Properly

2013-11-28 Thread Aamu Padi
Are you running the development server?


On Fri, Nov 29, 2013 at 12:30 AM, nagendra palla wrote:

> Hi Developers,
>
>I am new to Django App Development. My part-1 training is completed
> successfully. But when i go for part-2, my Admin page is not working
> properly. i.e., Admin page is loading with out CSS.
>
>
> 
>
> When i am open admin page it showing like above. I am running my django in
> Windows 8.
>
> What Software, i am used is:
>
> Python 2.7, Django 1.6, WAMP 2.4, WSGI
>
> and please find the attachment. i am attaching my project folder also.
> Please Help me Any one in this group.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e145c19f-6553-4efd-a1ae-8072d37f49b4%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWvyOundnjLjQ_AUn5VgTuVni1VTOEefg1ZETE7tRFjc1w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


How to get the class name of a ContentType in django

2013-11-28 Thread Aamu Padi
How do I get the class name in string of a ContentType? I tried it this
way, but it didn't worked out:

class StreamItem(models.Model):
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
pub_date = models.DateTimeField(default=datetime.now)

content_object = generic.GenericForeignKey('content_type', 'object_id')
content_class = content_type.__name__

def __unicode__(self):
return self.content_class

Any help will be much appreciated! Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtmzHebDbd%3DG01jtMzNaZ%3DF5HKbN0LWgzpHs6ghKuQeYg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Things to know about sorl-thumbnail

2013-11-10 Thread Aamu Padi
I am using sorl-thumbnail to create thumbnails for my project. I am
implementing it only in templates and not in the models or the view. And
each of the thumbnail are linked with their original image, which are used
by a lightbox. As a newbie, I wanted to know, some of its functionality:

   1. Whether implementing it only in the template is the same, as using it
   in the models or the view and creating a new thumbnail for each one?
   2. How to configure different thumbnails for different image, as it can
   be done in easy_thumbnail?
   3. How to override the default values, eg: override the value of Quality,
   etc.

And lastly, is it a correct way of implementing it? Any advice or
suggestion will be much appreciated. Thank you.

html:

{% for photo in photos %}


{% thumbnail photo.image "200x200" as im %}

{% endthumbnail %}


{% endfor %}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsSXqLyTtT6bVoYYts_wLsKVFfxi6Y1yw-bu7v6sND%2B9A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Get a set of objects from different models field names

2013-11-09 Thread Aamu Padi
Please have a look at my models.

class BackgroundImage(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

class ProfilePicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

class Album(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

class Meta:
ordering = ['-pub_date']
verbose_name_plural = ('Albums')

def __unicode__(self):
return self.name

class Photo(models.Model):
user = models.ForeignKey(User)
 album = models.ForeignKey(Album, default=3)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

How do I get all the *images* of *Photo*, *ProfilePicture* and
*BackgroundImage* from their *image field* in one set. And then *filter*them by
*-pub_date* to display in the template? Please help me out. Will be much
much appreciated! Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWt1r_hu7bCwnCQi5otrmHeSMW4mVvfK24LXbq1gcu6Ktw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


django - How to copy the actual image file from one model to another?

2013-11-08 Thread Aamu Padi
I want to copy images from one model to another within the project. Suppose
these are my models:

class BackgroundImage(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)


class ProfilePicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

@classmethod
def create_from_bg(cls, bg_img):
img = cls(user=bg_img.user, image=bg_img.image,
caption=bg_img.caption+'_copy', pub_date=bg_img.pub_date)
img.save()
return img

*For now, I can do these:*

*To get the user*
   * >>>m = User.objects.get(username='m')*

*To get the user's profile picture set*


*>>>m_pro_set = m.profilepicture_set.all()>>>m_pro_set
[]*

*Get an image object from Background image of the user*


*>>>m_back_1 = m.backgroundimage_set.get(id=2)>>>m_back_1
*

*And then:*
*>>>profile_pic = ProfilePicture.create_from_bg(m_back_1)*

*Now when I check it, it does create a new instance.*

*>>>m_pro_set[,]*


*But, if I check on the path, and even on the media folder, its the same
image and not an actual copy of the image file in the folder or disk.*





*>>>profile_pic.image>>>m_back_1.image*

What it does is, when I save a new instance of the image, it creates a new
instance of that same image, and not creates a new file in the disk, i.e.
there's no actual copying of the file in the directory. How do I go about
to actually copy the original image `file` within the models? Any help will
be much appreciated! Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWvovkBszoFPJJC_o_T1aENXB7GNpAH6NzpbOAg%2BmXbvpA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to copy and object from one model to another model

2013-11-05 Thread Aamu Padi
And how do I copy these images from one model to another?


On Wed, Nov 6, 2013 at 1:40 AM, Rafael E. Ferrero
<rafael.ferr...@gmail.com>wrote:

> if you see many similarities between two models will surely are the same
> thing. So BackgroundImage and ProfilePicture are the same thing and Photo
> its very similar to. I think you must to use inheritance of models [1] an
> use choices [2] for distinct types of pics
>
> I would do the following:
>
> class Pics(models.Model):
> PICS_TYPES = (
> ('PHOTOS', 'Photos'),
> ('BACKGROUNDS', 'Backgrounds'),
> ('PROFILES', 'Profiles')
> )
> user = models.ForeignKey(User)
> image = models.ImageField(upload_to=get_upload_file_name)
> caption = models.CharField(max_length=200)
> pub_date = models.DateTimeField(default=datetime.now)
> type = models.CharField(max_length=50, choices=PICS_TYPES)
>
>
> class Album(models.Model):
> user = models.ForeignKey(User)
> name = models.CharField(max_length=200)
> pub_date = models.DateTimeField(default=datetime.now)
>
>
> class Photo(Pics):
> album = models.ForeignKey(Album, default=3)
>
>
> [1]
> https://docs.djangoproject.com/en/1.5/topics/db/models/#model-inheritance
> [2]
> https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.Field.choices
>
>
>
> 2013/11/5 Aamu Padi <aamup...@gmail.com>
>
>> Each user has many *albums*, and each album has its many photos. And
>> each user has one *background image* to hold its many images. Similarly,
>> a user has one *profile picture* to hold its many images.
>>
>> These are my models:
>>
>> class UserProfile(models.Model):
>> user = models.OneToOneField(User)
>> permanent_address = models.TextField()
>> temporary_address = models.TextField()
>> profile_pic = models.ForeignKey(ProfilePicture)
>> background_pic = models.ForeignKey(BackgroundImage)
>>
>>
>> class Album(models.Model):
>> user = models.ForeignKey(User)
>> name = models.CharField(max_length=200)
>> pub_date = models.DateTimeField(default=datetime.now)
>>
>>
>> class Photo(models.Model):
>> album = models.ForeignKey(Album, default=3)
>> image = models.ImageField(upload_to=get_upload_file_name)
>> caption = models.CharField(max_length=200)
>> pub_date = models.DateTimeField(default=datetime.now)
>>
>>
>> class BackgroundImage(models.Model):
>> user = models.ForeignKey(User)
>> image = models.ImageField(upload_to=get_upload_file_name)
>> caption = models.CharField(max_length=200)
>> pub_date = models.DateTimeField(default=datetime.now)
>>
>>
>> class ProfilePicture(models.Model):
>> user = models.ForeignKey(User)
>> image = models.ImageField(upload_to=get_upload_file_name)
>> caption = models.CharField(max_length=200)
>> pub_date = models.DateTimeField(default=datetime.now)
>>
>> I tried doing this,  to let the user copy one image from *ProfilePicture*to 
>> *Background
>> Image*, but didn't work:
>>
>> # Get the user
>> >>>m = User.objects.get(username='m')
>>
>> # Get its set of Profile pictures
>> >>>m_pro_set = m.profilepicture_set.all()
>> >>>m_pro_set
>> []
>>
>> # Get its set of Background images
>> >>>m_back_set = m.backgroundimage_set.all()
>> >>>m_back_set
>> []
>>
>> # Get an image object from Profile picture of the user
>>  >>>m_pro_1 = m.profilepicture_set.get(id=2)
>> >>>m_pro_1
>> 
>>
>> # Get an image object from Background image of the user
>> >>>m_back_1 = m.backgroundimage_set.get(id=2)
>> >>>m_back_1
>> 
>>
>> # So, I tried to copy one image from *BackgroundImage* of a user *to*
>> *ProfilePicture*
>>
>> >>>m_pro_set.objects.create(m_back_1)
>>
>>
>> *File "", line 1, object has attribute 'objects'*
>> *AttributeError: 'QuerySet' object has no attribute 'objects'*
>>
>> So my question is, how to copy an objects from one model to another? Any
>> advice will be much appreciated. Thank you!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit t

Re:

2013-11-05 Thread Aamu Padi
Thank you all for the precious ideas! I made some changes and just about to
get the result. :D
Thank you again!


On Mon, Nov 4, 2013 at 6:22 PM, Tom Evans <tevans...@googlemail.com> wrote:

> On Sun, Nov 3, 2013 at 8:23 PM, Aamu Padi <aamup...@gmail.com> wrote:
> > How to make a model with a yes/no field, if yes that particular object
> > (image) will be used as the background, and no other image will be
> selected.
> >
> > I have come with this model:
> >
> > class BackgroundImage(models.Model):
> > user = models.ForeignKey(user)
> > caption = models.CharField(max_length=200)
> > image = models.ImageField(upload_to=get_upload_file_name)
> > using_image = models.BooleanField()
> >
> > But with this I can select all of them, and not only one.
> >
> > Lets take an example of profile picture. A user can have many images,
> but he
> > can only select one as a profile picture. So, that if he selects one, the
> > other will be un-select itself.
> > Hope everybody understood what I mean. Please ask me if didn't
> understood.
> > Also please correct me if my model is not correct. Thank you!
> >
>
> User can have many images, but only one profile image:
>
> class User:
>   profile_image = ForeignKey('BackgroundImage', blank=True, null=True)
>   # other fields
>
> class BackgroundImage:
>   owner = ForeignKey('User')
>   # other fields
>
> # get a profile pic
> image_url = user.profile_pic and user.profile_pic.image.url or ''
>
> # set a new profile pic
> image = BackgroundImage()
> user.profile_pic = image
> user.save()
>
> User can have many images, but only one profile image, without
> modifying user model
>
> class User:
>   # other fields
>
> class BackgroundImage:
>   owner = ForeignKey('User')
>   # other fields
>
> class UserBackgroundImage:
>   user = OneToOneField('User')
>   profile_picture = ForeignKey('BackgroundImage')
>
> # get a profile pic
> image_url = user.userbackgroundimage and
> user.backgroundimage.profile_picture.image.url or ''
>
> # set a new profile pic
> image = BackgroundImage()
> user_image, created =
> UserBackgroundImage.objects.get_or_create(user=user,
> defaults={'profile_picture': image})
> if not created:
>   user_image.profile_picture = image
>   user_image.save()
>
> Cheers
>
> Tom
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAFHbX1%2Bng1g2OtAtsTb43V-JFbetP5WVtvUO_8PWUzjAFPs%3DCQ%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWstN3b_3L%2B0s4F1%3D4EJxK_WOk0J_-%3DbOym3WAUrGmP8xA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


How to copy and object from one model to another model

2013-11-05 Thread Aamu Padi
Each user has many *albums*, and each album has its many photos. And each
user has one *background image* to hold its many images. Similarly, a user
has one *profile picture* to hold its many images.

These are my models:

class UserProfile(models.Model):
user = models.OneToOneField(User)
permanent_address = models.TextField()
temporary_address = models.TextField()
profile_pic = models.ForeignKey(ProfilePicture)
background_pic = models.ForeignKey(BackgroundImage)


class Album(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)


class Photo(models.Model):
album = models.ForeignKey(Album, default=3)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)


class BackgroundImage(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)


class ProfilePicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)

I tried doing this,  to let the user copy one image from
*ProfilePicture*to *Background
Image*, but didn't work:

# Get the user
>>>m = User.objects.get(username='m')

# Get its set of Profile pictures
>>>m_pro_set = m.profilepicture_set.all()
>>>m_pro_set
[]

# Get its set of Background images
>>>m_back_set = m.backgroundimage_set.all()
>>>m_back_set
[]

# Get an image object from Profile picture of the user
>>>m_pro_1 = m.profilepicture_set.get(id=2)
>>>m_pro_1


# Get an image object from Background image of the user
>>>m_back_1 = m.backgroundimage_set.get(id=2)
>>>m_back_1


# So, I tried to copy one image from *BackgroundImage* of a user *to*
*ProfilePicture*

>>>m_pro_set.objects.create(m_back_1)


*File "", line 1, object has attribute 'objects'*
*AttributeError: 'QuerySet' object has no attribute 'objects'*

So my question is, how to copy an objects from one model to another? Any
advice will be much appreciated. Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWue7HtdGuJeqtEPsa%2Bncc2rrpE_tEuZSXRA4NXZtsmxkA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to add objects from one class to another

2013-11-05 Thread Aamu Padi
Ok, I guess, I will try that. Thank you!!!


On Tue, Nov 5, 2013 at 10:40 PM, Gonzalo Delgado <m...@gonzalodelgado.com.ar
> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> El 05/11/13 13:14, Aamu Padi escribió:
> > I think abstract models is what I am after. But, I also want to get
> > the latest photo by that user, be that a profile pic or background
> > pic. Can I use the abstract base model, ie the Image model to
> > atleast get the latest photo by [-pub_date], even though I wont
> > save anything in it???
>
> Hello Aamu :-)
>
> Sadly, you won't be able to retrieve data in any way using the
> abstract model class (see
>
> https://docs.djangoproject.com/en/1.5/topics/db/models/#abstract-base-classes
> ), but django-polymorphic can definitely do that.
>
> - --
> Gonzalo Delgado
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.15 (Darwin)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iF4EAREIAAYFAlJ5JpgACgkQzbfdFL5JoUnlKAD+LljmaN7XpFTdQtcZx4i9e43c
> 7hAZ8Y2XOv0tH5TljKcA+wQdh5fNUmk71laONL1bWBWizncxe2PGFO/+QsLyOP+e
> =AI7P
> -END PGP SIGNATURE-
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/52792698.6040509%40gonzalodelgado.com.ar
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsgtvbC574jh25rtFqnf92bNrA%3DmpDyniAYavaHO3jXjw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to add objects from one class to another

2013-11-05 Thread Aamu Padi
Hello, Gonzalo! :D
I think abstract models is what I am after. But, I also want to get the
latest photo by that user, be that a profile pic or background pic. Can I
use the abstract base model, ie the Image model to atleast get the latest
photo by [-pub_date], even though I wont save anything in it???


On Tue, Nov 5, 2013 at 6:18 AM, Gonzalo Delgado
<m...@gonzalodelgado.com.ar>wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> El 04/11/13 19:33, Aamu Padi escribió:
> > This is my models.py:
> >
> > class Image(models.Model): user = models.ForeignKey(User) caption =
> > models.CharField(max_length=300) image =
> > models.ImageField(upload_to=get_upload_file_name) pub_date =
> > models.DateTimeField(default=datetime.now)
> >
> > class Meta: ordering = ['-pub_date'] verbose_name_plural =
> > ('Images')
> >
> > def __unicode__(self): return "%s - %s" % (self.caption,
> > self.user.username)
> >
> >
> > class ProfilePic(Image): pass
> >
> >
> >
> > class BackgroundPic(Image): pass
> >
> >
> > class Album(models.Model): name = models.CharField(max_length=150)
> >
> > def __unicode__(self): return self.name
> >
> >
> > class Photo(Image): album = models.ForeignKey(Album, default=3)
> >
> > And this is another:
> >
> > class UserProfile(models.Model): user = models.OneToOneField(User)
> > permanent_address = models.TextField() temporary_address =
> > models.TextField() profile_pic = models.ForeignKey(ProfilePic)
> > background_pic = models.ForeignKey(BackgroundPic)
> >
> > def __unicode__(self): return self.user.username
> >
> > I can access the Parent class with its User object.
> >
> >>>> m = User.objects.get(username='mika') m.image_set.all()
> >>>> [, ,
> >>>>  > mika_bf - mika>]
> >
> >
> > But I can't access its child class with the User. I tried:
> >
> >>>> m.Image.profilepic_set.all()
> > and
> >
> >>>> m.image_set.profilpic.all()
> >
> > and this
> >
> >>>> m.profilepic_set.all()
> > AttributeError:'User' object has no attribute 'profilepic_set'
> >
> > But all gave me errors!  How do I access the child classes, so that
> > I can add images from one class to another. Like copy one image
> > from Photo to ProfilePic, or from ProfilePic to BackgroundPic and
> > so. Or simply, how to add images for particular User in specific
> > classes?
>
> I don't think Django supports that kind of queries. You can try
> django-polymorphic for something very close to what you want:
> https://github.com/chrisglass/django_polymorphic
>
> My recommendation, though, would be to not use inheritance that way
> and simply add an "image_type" field to your Image model and filter
> that instead:
>
>  class Image(models.Model):
>  user = models.ForeignKey(User)
>  caption = models.CharField(max_length=300)
>  image = models.ImageField(upload_to=get_upload_file_name)
>  pub_date = models.DateTimeField(default=datetime.now)
>  image_type = models.CharField(
>  max_length=20, choices=(('background', 'Background'),
>  ('profile', 'Profile')))
>
> and do:
> m.image_set.filter(image_type='background')
> or:
> m.image_set.filter(image_type='profile')
>
>
> Another possibility would be to make Image an abstract model. Your
> references to profilepic_set and backgroundpic_set will work, but you
> won't be able to use the Image model to store data in the database.
>
>
> - --
> Gonzalo Delgado
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.15 (Darwin)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iF4EAREIAAYFAlJ4QE8ACgkQzbfdFL5JoUlCDAEAkyrstji355TouLYZDjXmax8x
> mFnN0nQ2lDbo0lUNiBsA/27hAQNhX74p1Wb3O1e3qjkK23nj6NCL09GL167Knl0h
> =5oiE
> -END PGP SIGNATURE-
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5278404F.8080805%40gonzalodelgado.com.ar
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWv5iUykR83YpxbtNsJF_kAHjvyNjKXRD5x72vM8OrnvGg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to add objects from one class to another

2013-11-04 Thread Aamu Padi
What I want is that, each user will have a set of `profile pictures`,
`background images` and other set of `photos uploaded`. These images will
be kept separately in the templates. And if the user wants, he can easily
use (copy) the other set of photos uploaded or images from the background
image set, as the profile picture, and that image will be added to `profile
picture set`. Or if he wants, he can use an image from `profile picture
set` or from the other `uploaded photos` as the `background image` and
similarly that image used from other class will be copied to `background
image set`.


On Tue, Nov 5, 2013 at 4:03 AM, Aamu Padi <aamup...@gmail.com> wrote:

> This is my models.py:
>
> class Image(models.Model):
> user = models.ForeignKey(User)
> caption = models.CharField(max_length=300)
> image = models.ImageField(upload_to=get_upload_file_name)
> pub_date = models.DateTimeField(default=datetime.now)
>
> class Meta:
> ordering = ['-pub_date']
> verbose_name_plural = ('Images')
>
> def __unicode__(self):
> return "%s - %s" % (self.caption, self.user.username)
>
>
> class ProfilePic(Image):
> pass
>
>
>
> class BackgroundPic(Image):
> pass
>
>
> class Album(models.Model):
> name = models.CharField(max_length=150)
>
> def __unicode__(self):
> return self.name
>
>
> class Photo(Image):
> album = models.ForeignKey(Album, default=3)
>
> And this is another:
>
> class UserProfile(models.Model):
> user = models.OneToOneField(User)
> permanent_address = models.TextField()
> temporary_address = models.TextField()
> profile_pic = models.ForeignKey(ProfilePic)
> background_pic = models.ForeignKey(BackgroundPic)
>
> def __unicode__(self):
> return self.user.username
>
> I can access the Parent class with its User object.
>
> >>>m = User.objects.get(username='mika')
> >>>m.image_set.all()
> >>>[, ,  mika_bf - mika>]
>
>
> But I can't access its child class with the User. I tried:
>
> >>>m.Image.profilepic_set.all()
> and
>
> >>>m.image_set.profilpic.all()
>
> and this
>
> >>>m.profilepic_set.all()
> AttributeError:'User' object has no attribute 'profilepic_set'
>
> But all gave me errors!  How do I access the child classes, so that I can
> add images from one class to another. Like copy one image from Photo to
> ProfilePic, or from ProfilePic to BackgroundPic and so. Or simply, how to
> add images for particular User in specific classes?
>
> Please guide me to achieve the above mentioned. Will be much appreciated.
> Thank you.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuofMKV1PLdcPJ8FTk4BsHiBAC2VrOQ4xZw4MNUd1U4uw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


How to add objects from one class to another

2013-11-04 Thread Aamu Padi
This is my models.py:

class Image(models.Model):
user = models.ForeignKey(User)
caption = models.CharField(max_length=300)
image = models.ImageField(upload_to=get_upload_file_name)
pub_date = models.DateTimeField(default=datetime.now)

class Meta:
ordering = ['-pub_date']
verbose_name_plural = ('Images')

def __unicode__(self):
return "%s - %s" % (self.caption, self.user.username)


class ProfilePic(Image):
pass



class BackgroundPic(Image):
pass


class Album(models.Model):
name = models.CharField(max_length=150)

def __unicode__(self):
return self.name


class Photo(Image):
album = models.ForeignKey(Album, default=3)

And this is another:

class UserProfile(models.Model):
user = models.OneToOneField(User)
permanent_address = models.TextField()
temporary_address = models.TextField()
profile_pic = models.ForeignKey(ProfilePic)
background_pic = models.ForeignKey(BackgroundPic)

def __unicode__(self):
return self.user.username

I can access the Parent class with its User object.

>>>m = User.objects.get(username='mika')
>>>m.image_set.all()
>>>[, , ]


But I can't access its child class with the User. I tried:

>>>m.Image.profilepic_set.all()
and

>>>m.image_set.profilpic.all()

and this

>>>m.profilepic_set.all()
AttributeError:'User' object has no attribute 'profilepic_set'

But all gave me errors!  How do I access the child classes, so that I can
add images from one class to another. Like copy one image from Photo to
ProfilePic, or from ProfilePic to BackgroundPic and so. Or simply, how to
add images for particular User in specific classes?

Please guide me to achieve the above mentioned. Will be much appreciated.
Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsBg1sgWTQF%3D5zzKXiz2-yt8twVDnv8xP1bM2tJeZF4qw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Model to select only 1 object from multiple objects

2013-11-04 Thread Aamu Padi
Thank you. I hope I can walk through now. :)


On Mon, Nov 4, 2013 at 7:43 PM, Ruturaj Dhekane <rutura...@gmail.com> wrote:

> Your model can work. But I think the model there is an alternate way to
> create this model.
>
> The way I might design this is have a profile model
> Profile = name, birth date, location, favorite books, friends,
> ProfilePicture (foreignKey)
>
> ProfilePicture = image, caption
> So now, you can have multiple profile pictures and the profile will point
> to that profile picture which needs to be shown. Hope this helps.
>
> To do it the way you are doing - you need to make sure that first all
> User=X have set using_image=false. Then set the particular used
> using_image=true. Then you can retrieve the unique photo for every user by
> filtering on using_image = true This needs to be done in code rather than
> in model.
>
>
> On Mon, Nov 4, 2013 at 1:57 AM, Aamu Padi <aamup...@gmail.com> wrote:
>
>> How to make a model with a yes/no field, if yes that particular object
>> (image) will be used as the background, and no other image will be
>> selected.
>>
>> I have come with this model:
>>
>> class BackgroundImage(models.Model):
>> user = models.ForeignKey(user)
>> caption = models.CharField(max_length=
>> 200)
>> image = models.ImageField(upload_to=get_upload_file_name)
>> using_image = models.BooleanField()
>>
>> But with this I can select all of them, and not only one.
>>
>> Lets take an example of profile picture. A user can have many images, but
>> he can only select one as a profile picture. So, that if he selects one,
>> the other will be un-select itself.
>> Hope everybody understood what I mean. Please ask me if didn't
>> understood. Also please correct me if my model is not correct. Thank you!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAHSNPWtW89SBNRnCh_EO6Uj1hTPhixsxSRwq%3DhHmuGzdTJfWAg%40mail.gmail.com
>> .
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEGxL4mu-%3DNkLMFz96m5tVr2%3DAggDh470%3DnRp7dKqWEgUvTQgQ%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWu3sGB%3Dexcqy_fSOpOKd%2B-WBeeoU3mm_v9b1j7iuGhMsg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re:

2013-11-03 Thread Aamu Padi
Thank you for the reply Lee. But could please elaborate a little more. :)


On Mon, Nov 4, 2013 at 2:48 AM, Lee Hinde <leehi...@gmail.com> wrote:

>
> On Nov 3, 2013, at 12:23 PM, Aamu Padi <aamup...@gmail.com> wrote:
>
> > How to make a model with a yes/no field, if yes that particular object
> (image) will be used as the background, and no other image will be selected.
> >
> > I have come with this model:
> >
> > class BackgroundImage(models.Model):
> > user = models.ForeignKey(user)
> > caption = models.CharField(max_length=200)
> > image = models.ImageField(upload_to=get_upload_file_name)
> > using_image = models.BooleanField()
> >
> > But with this I can select all of them, and not only one.
> >
> > Lets take an example of profile picture. A user can have many images,
> but he can only select one as a profile picture. So, that if he selects
> one, the other will be un-select itself.
> > Hope everybody understood what I mean. Please ask me if didn't
> understood. Also please correct me if my model is not correct. Thank you!
> >
>
> I recently needed a similar thing (one item out of a table being _the_
> thing) and decided that host table was the wrong place to store it. It
> needs to be in the other table, in this case, your user table should have a
> field that is the key of the profile picture.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/D75EAE65-8A05-4E92-9638-DD89D852800F%40gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtWQfDH%2BzXTtqmCTWS1ZwoowKPQqiFvr7yL2%3DOr1xMLdQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Model to select only 1 object from multiple objects

2013-11-03 Thread Aamu Padi
How to make a model with a yes/no field, if yes that particular object
(image) will be used as the background, and no other image will be
selected.

I have come with this model:

class BackgroundImage(models.Model):
user = models.ForeignKey(user)
caption = models.CharField(max_length=
200)
image = models.ImageField(upload_to=get_upload_file_name)
using_image = models.BooleanField()

But with this I can select all of them, and not only one.

Lets take an example of profile picture. A user can have many images, but
he can only select one as a profile picture. So, that if he selects one,
the other will be un-select itself.
Hope everybody understood what I mean. Please ask me if didn't understood.
Also please correct me if my model is not correct. Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWtW89SBNRnCh_EO6Uj1hTPhixsxSRwq%3DhHmuGzdTJfWAg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


[no subject]

2013-11-03 Thread Aamu Padi
How to make a model with a yes/no field, if yes that particular object
(image) will be used as the background, and no other image will be
selected.

I have come with this model:

class BackgroundImage(models.Model):
user = models.ForeignKey(user)
caption = models.CharField(max_length=200)
image = models.ImageField(upload_to=get_upload_file_name)
using_image = models.BooleanField()

But with this I can select all of them, and not only one.

Lets take an example of profile picture. A user can have many images, but
he can only select one as a profile picture. So, that if he selects one,
the other will be un-select itself.
Hope everybody understood what I mean. Please ask me if didn't understood.
Also please correct me if my model is not correct. Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWvAr2iVH7MT1TJDmBqOO3EmFt4NR%3DYv1OQY0sexLUKK8w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: django - default value name for the ForeignKey

2013-10-31 Thread Aamu Padi
Thank you so much for your reply. But I am just a beginer, didn't quite
understood the 'Manger' thing. Will you please be kind enough to elaborate
a little more. Thank you.


On Thu, Oct 31, 2013 at 8:44 PM, Aamu Padi <aamup...@gmail.com> wrote:

> I have a model for uploading photos in an album by the users. But if the
> user don't want to create an album, and just upload photos, I want it to be
> uploaded in a default album, namely 'Default album'. So that, whenever the
> user uploads photo without creating a new album, the 'Default album should
> be updated by that photos. For that, I thought about giving a default
> value in the title of the Album, but I also don't want to create a new
> album with name 'Default album' whenever the user doesn't create a new
> album to upload photo. I just want to update the 'Default Album'. Please
> suggest me how to achieve the above mentioned. And also is it okay to just
> give the ForeignKey field of the User to the Album class and not the
> Photo class??? Thank you!
>
> models.py:
>
> class Album(models.Model):
> title = models.CharField(max_length=200)
> pub_date = models.DateTimeField(default=datetime.now)
> user = models.ForeignKey(User)
>
> class Photo(models.Model):
> album = models.ForeignKey(Album)
> title = models.CharField(max_length=200)
> image = models.ImageField(upload_to=get_upload_file_name, blank=True)
> pub_date = models.DateTimeField(default=datetime.now)
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsH%3DsVO7WJFRp2t8BZzu170-fD_ytamp-arN%2BCkEG639g%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


django - default value name for the ForeignKey

2013-10-31 Thread Aamu Padi
I have a model for uploading photos in an album by the users. But if the
user don't want to create an album, and just upload photos, I want it to be
uploaded in a default album, namely 'Default album'. So that, whenever the
user uploads photo without creating a new album, the 'Default album should
be updated by that photos. For that, I thought about giving a default
valuein the title of the Album, but I also don't want to create a new
album with
name 'Default album' whenever the user doesn't create a new album to upload
photo. I just want to update the 'Default Album'. Please suggest me how to
achieve the above mentioned. And also is it okay to just give the ForeignKey
field of the User to the Album class and not the Photo class??? Thank you!

models.py:

class Album(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
user = models.ForeignKey(User)

class Photo(models.Model):
album = models.ForeignKey(Album)
title = models.CharField(max_length=200)
image = models.ImageField(upload_to=get_upload_file_name, blank=True)
pub_date = models.DateTimeField(default=datetime.now)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWs-6Lx%3Dtwwb0Y%2B7zd4wn-7DEqXUR7%2BE4Ttz4C3wukqFgQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: django difference between - one to one, many to one and many to many

2013-10-30 Thread Aamu Padi
Thank you! That did helped.

On Tuesday, October 29, 2013 12:36:20 AM UTC+5:30, Aamu Padi wrote:
>
> So, this is my first time learning computer language. And I chose python 
> and django. Now, I got many of the basic concepts of python and also 
> django. I can create new page with the views and all other stuff. But I am 
> still confused with the relations, i.e. one to one, many to one, and many 
> to many. Will someone please please please explain it to me. How can I use 
> it? I really need to know.
>
> *N.B:* I have looked at this 
> link<https://docs.djangoproject.com/en/dev/topics/db/models/#relationships>for
>  that, but 
> I still have some doubts about one-to-one and many-to-many. Suppose I 
> want to extend with some more user information in a different class. Can I 
> use ForeignKey instead of One-to-One field? And will anyone please be 
> kind enough to explain to me about many-to-many relationship, (I didn't 
> quit understood all those toppings and membership thing). 
>
> Thanks.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0ab4c921-9226-4592-951c-3e5c8ed6b775%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: django difference between - one to one, many to one and many to many

2013-10-29 Thread Aamu Padi
Thank you so much! I will look into that! :)

On Tuesday, October 29, 2013 1:37:37 AM UTC+5:30, C. Kirby wrote:
>
> Those concepts are actually RDBMS (Database) ideas. You would probably be 
> better off reading up on those. This looks to be a pretty good explanation:
>
> http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/
>
>
> On Monday, October 28, 2013 2:06:20 PM UTC-5, Aamu Padi wrote:
>>
>> So, this is my first time learning computer language. And I chose python 
>> and django. Now, I got many of the basic concepts of python and also 
>> django. I can create new page with the views and all other stuff. But I am 
>> still confused with the relations, i.e. one to one, many to one, and many 
>> to many. Will someone please please please explain it to me. How can I use 
>> it? I really need to know.
>>
>> *N.B:* I have looked at this 
>> link<https://docs.djangoproject.com/en/dev/topics/db/models/#relationships>for
>>  that, but 
>> I still have some doubts about one-to-one and many-to-many. Suppose I 
>> want to extend with some more user information in a different class. Can I 
>> use ForeignKey instead of One-to-One field? And will anyone please be 
>> kind enough to explain to me about many-to-many relationship, (I didn't 
>> quit understood all those toppings and membership thing). 
>>
>> Thanks.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/859a5ba6-34d1-4994-9e55-1ceb81549a50%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


django difference between - one to one, many to one and many to many

2013-10-28 Thread Aamu Padi
 

So, this is my first time learning computer language. And I chose python 
and django. Now, I got many of the basic concepts of python and also 
django. I can create new page with the views and all other stuff. But I am 
still confused with the relations, i.e. one to one, many to one, and many 
to many. Will someone please please please explain it to me. How can I use 
it? I really need to know.

*N.B:* I have looked at this 
linkfor 
that, but 
I still have some doubts about one-to-one and many-to-many. Suppose I want 
to extend with some more user information in a different class. Can I use 
ForeignKey instead of One-to-One field? And will anyone please be kind 
enough to explain to me about many-to-many relationship, (I didn't quit 
understood all those toppings and membership thing). 

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/baa3-232d-4614-ac1e-4ba04adc9c16%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.