from django.db import models
from django.conf import settings

class Contact(models.Model):
    user_from = models.ForeignKey('auth.User',
                                  related_name='rel_from_set',
								  on_delete=models.CASCADE)
    user_to = models.ForeignKey('auth.User',
                                related_name='rel_to_set',
								on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True,
                                   db_index=True)
	
	
    class Meta:
        ordering = ('-created',)
   
    def __str__(self):
        return '{} obserwuje {}'.format(self.user_from,
                                      self.user_to)



class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE)
    date_of_birth = models.DateField(blank=True, null=True)
    photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)
    
    def __str__(self):
        return 'Profil użytkownika {}.'.format(self.user.username)

		
# Dynamiczne dodanie poniższej kolumny do modelu User.
    from django.contrib.auth.models import User

    User.add_to_class('following',
                  models.ManyToManyField('self',
                                         through=Contact,
                                         related_name='followers',
                                         symmetrical=False))